コード例 #1
0
 //----------------------------------------------------------------------------------
 // Iteration using Queue
 public static IList<IList<int>> LevelOrderIteration(TreeNode root)
 {
     IList<IList<int>> res = new List<IList<int>>();
     if (root == null)
         return res;
     Queue<TreeNode> queue = new Queue<TreeNode>();
     queue.Enqueue(root);
     int expect = 1;  // total number of nodes in current level
     int visit = 0;  // number of visited nodes in current level
     IList<int> level = new List<int>();  // current level
     while (queue.Count != 0)
     {
         TreeNode curr = queue.Dequeue();
         level.Add(curr.val);
         visit++;
         if (curr.left != null)
             queue.Enqueue(curr.left);
         if (curr.right != null)
             queue.Enqueue(curr.right);
         if (visit == expect)
         {
             expect = queue.Count;
             visit = 0;
             res.Add(level);
             level = new List<int>();
         }
     }
     return res;
 }
コード例 #2
0
 public static int GetClosestValue(TreeNode root, double target)
 {
     if ((double)root.val == target) return root.val;
     TreeNode child = target < root.val ? root.left : root.right;
     if (child == null) return root.val;
     int temp = GetClosestValue(child, target);
     return Math.Abs(temp - target) < Math.Abs(root.val - target) ? temp : root.val;
 }
コード例 #3
0
 // return the max path sum starting from root
 // and update the global max during process
 private int DFS(TreeNode root)
 {
     if (root == null) return 0;
     // ignore children path with negative sum
     int leftPath = Math.Max(0, DFS(root.left));  // simple path rooting at left child
     int rightPath = Math.Max(0, DFS(root.right));  // simple path rooting at right child
     maxSum = Math.Max(maxSum, leftPath + rightPath + root.val);
     return Math.Max(leftPath, rightPath) + root.val;
 }
コード例 #4
0
 private void DFS(TreeNode root, int pathSum)
 {
     if (root == null) return;
     if (root.left == null && root.right == null)
     {
         treeSum += pathSum * 10 + root.val;
         return;
     }
     pathSum = pathSum * 10 + root.val;
     DFS(root.left, pathSum);
     DFS(root.right, pathSum);
 }
コード例 #5
0
        // Question: #103
        public static IList<IList<int>> ZigzagTraverse(TreeNode root)
        {
            if (root == null) return new List<IList<int>>();

            IList<IList<int>> res = new List<IList<int>>();
            // for nodes in odd level
            Stack<TreeNode> oddStack = new Stack<TreeNode>();
            // for nodes in even level
            Stack<TreeNode> evenStack = new Stack<TreeNode>();
            oddStack.Push(root);

            while (oddStack.Count != 0 || evenStack.Count != 0)
            {
                IList<int> level = new List<int>();
                while (oddStack.Count != 0)
                {
                    TreeNode odd = oddStack.Pop();
                    level.Add(odd.val);
                    if (odd.left != null)
                    {
                        evenStack.Push(odd.left);
                    }
                    if (odd.right != null)
                    {
                        evenStack.Push(odd.right);
                    }
                }
                if (level.Count != 0)
                {
                    res.Add(level);
                    level = new List<int>();
                }
                while (evenStack.Count != 0)
                {
                    TreeNode even = evenStack.Pop();
                    level.Add(even.val);
                    if (even.right != null)
                    {
                        oddStack.Push(even.right);
                    }
                    if (even.left != null)
                    {
                        oddStack.Push(even.left);
                    }
                }
                if (level.Count != 0)
                {
                    res.Add(level);
                }
            }
            return res;
        } 
コード例 #6
0
        private static void DFS(TreeNode p, IList<IList<int>> res, int level)
        {
            if (p == null) return;
            if (res.Count <= level)
            {
                IList<int> newLevel = new List<int>();
                res.Add(newLevel);
            }
            IList<int> curr = res[level];
            curr.Add(p.val);

            DFS(p.left, res, level + 1);
            DFS(p.right, res, level + 1);
        }
コード例 #7
0
 public static IList<IList<int>> LevelOrderBottom(TreeNode root)
 {
     IList<IList<int>> res = new List<IList<int>>();
     DFS(root, res, 0);
     // reverse
     int mid = res.Count / 2;
     for (int i = 0; i < mid; i++)
     {
         IList<int> temp = res[i];
         res[i] = res[res.Count - i - 1];
         res[res.Count - i - 1] = temp;
     }
     return res;
 }
コード例 #8
0
ファイル: Path.cs プロジェクト: LianghongWu/CourseWork
 private void DFS(TreeNode root, StringBuilder path)
 {
     if (root == null) return;
     if (path.Length > 0)
     {
         path.Append("->");
     }
     path.Append(root.val);
     if (root.left == null && root.right == null)
     {
         res.Add(path.ToString());
     }
     DFS(root.left, new StringBuilder(path.ToString()));
     DFS(root.right, new StringBuilder(path.ToString()));
 }
コード例 #9
0
ファイル: PathSum.cs プロジェクト: LianghongWu/CourseWork
 private bool DFS(TreeNode root, int pathRemain)
 {
     if (root == null) return false;
     // leaf node
     if (root.left == null && root.right == null)
     {
         if (root.val == pathRemain)
         {
             return true;
         }
         return false;
     }
     pathRemain -= root.val;
     return DFS(root.left, pathRemain) || DFS(root.right, pathRemain);
 }
コード例 #10
0
 public TreeNode GetLowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
 {
     if (root == null)
         return root;
     if (p == null || q == null)
         return root;
     if (p == root || q == root)
         return root;
     if ((p.val < root.val && q.val > root.val)
         || (p.val > root.val && q.val < root.val))
         return root;
     if (p.val < root.val && q.val < root.val)
     {
         return GetLowestCommonAncestor(root.left, p, q);
     }
     else
     {
         return GetLowestCommonAncestor(root.right, p, q);
     }
 }
コード例 #11
0
ファイル: Path.cs プロジェクト: LianghongWu/CourseWork
 public IList<string> BinaryTreePaths(TreeNode root)
 {
     DFS(root, new StringBuilder());
     return res;
 }
コード例 #12
0
 public int SumNumbers(TreeNode root)
 {
     DFS(root, 0);
     return treeSum;
 }
コード例 #13
0
ファイル: PathSum.cs プロジェクト: LianghongWu/CourseWork
 public bool HasPathSum(TreeNode root, int sum)
 {
     return DFS(root, sum);
 }
コード例 #14
0
 // Recursion using DFS
 public static IList<IList<int>> LevelOrder(TreeNode root)
 {
     IList<IList<int>> res = new List<IList<int>>();
     DFS(root, res, 0);
     return res;
 }
コード例 #15
0
 public int MaxPathSum(TreeNode root)
 {
     DFS(root);
     return maxSum;
 }
コード例 #16
0
        //-------------------------------------------------------------------------------------------------
        // O(n) space method
        // using extra space with queue and stack
        public static List<int> TraverseWithSimpleNode(TreeNode root) {
            if (root == null) return new List<int>();

            List<int> res = new List<int>();
            Queue<TreeNode> queue = new Queue<TreeNode>();
            Stack<TreeNode> stack = new Stack<TreeNode>();
            stack.Push(root);

            while (stack.Count != 0 || queue.Count != 0) {
                while (stack.Count != 0) {
                    TreeNode p = stack.Pop();
                    res.Add(p.val);
                    if (p.left != null) queue.Enqueue(p.left);
                    if (p.right != null) queue.Enqueue(p.right);
                }
                while (queue.Count != 0) {
                    TreeNode q = queue.Dequeue();
                    res.Add(q.val);
                    if (q.left != null) stack.Push(q.left);
                    if (q.right != null) stack.Push(q.right);
                }
            }
            return res;
        }
コード例 #17
0
 private int Height(TreeNode root)
 {
     if (root == null) return 0;
     return Math.Max(Height(root.left), Height(root.right)) + 1;
 }
コード例 #18
0
 /*
  * #110
  */
 public bool IsBalanced(TreeNode root)
 {
     if (root == null) return true;
     return IsBalanced(root.left) && IsBalanced(root.right) && Math.Abs(Height(root.left) - Height(root.right)) < 2;
 }