Ejemplo n.º 1
0
        public int MinDepth(TreeNode root)
        {
            if (root == null) return 0;
            int left = MinDepth(root.left);
            int right = MinDepth(root.right);

            return (left == 0 || right == 0) ? left + right + 1 : Math.Min(left, right) + 1;
        }
Ejemplo n.º 2
0
 public int MaxDepth(TreeNode root)
 {
     if (root == null)
     {
         return 0;
     }
     return 1+ Math.Max(MaxDepth(root.left), MaxDepth(root.right));
 }
Ejemplo n.º 3
0
        public IList<IList<int>> LevelOrder(TreeNode root)
        {
            IList<IList<int>> result = new List<IList<int>>();

            if (root == null) return result;

            Dfs(root, 0, ref result);
            return result;
        }
Ejemplo n.º 4
0
 public bool Dfs(TreeNode root, int sum)
 {
     if (root == null) return false;
     if (root.left == null && root.right == null)
     {
         return root.val == sum;
     }
     return Dfs(root.left, sum - root.val) || Dfs(root.right, sum - root.val);
 }
Ejemplo n.º 5
0
        public IList<IList<int>> LevelOrderBottom(TreeNode root)
        {
            IList<IList<int>> result = new List<IList<int>>();

            if (root == null) return result;

            int level = 0;
            Dfs(root, level, ref result);

            return result.Reverse().ToList();
        }
Ejemplo n.º 6
0
        public void Dfs(TreeNode root, int level, ref IList<IList<int>> result)
        {
            if (root == null) return;
            
            if (result.Count == level) result.Add(new List<int>());

            result[level].Add(root.val);

            Dfs(root.left, level + 1, ref result);
            Dfs(root.right, level + 1, ref result);
        }
Ejemplo n.º 7
0
        // Definition for a binary tree node.

        public TreeNode InvertTree(TreeNode root)
        {
            if (root == null)
                return null;
            TreeNode node = root.left;
            root.left = root.right;
            root.right = node;
            InvertTree(root.left);
            InvertTree(root.right);
            return root;
        }
Ejemplo n.º 8
0
        public void Execute()
        {
            TreeNode root = new TreeNode(1);
            root.left = new TreeNode(2)
            {
                left = new TreeNode(4),
                right = new TreeNode(5)
            };

            root.right = new TreeNode(3)
            {

            };

            LevelOrderBottom(root);
            Console.WriteLine("Hell!");
        }
Ejemplo n.º 9
0
        public void Execute()
        {
            TreeNode root = new TreeNode(1);
            root.left = new TreeNode(2)
            {
                left = new TreeNode(4),
                right = new TreeNode(5)
            };

            root.right = new TreeNode(3)
            {

            };

            bool result = HasPathSum(root, 18);
            Console.WriteLine(result);
        }
Ejemplo n.º 10
0
        public bool IsSymmetric(TreeNode root1, TreeNode root2)
        {

//            while (root1 != null && root2 != null)
//            {
//                
//            }

            if (root1 == null && root2 == null)
            {
                return true;
            }

            if (root1 != null && root2 != null)
            {
                if (root1.val == root2.val)
                {
                    return IsSymmetric(root1.left, root2.right) && IsSymmetric(root1.right, root2.left);
                }
            }
            return false;
        }
Ejemplo n.º 11
0
        public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
        {
            int big, small;
            if (p.val > q.val)
            {
                big = p.val;
                small = q.val;
            }
            else
            {
                big = q.val;
                small = p.val;
            }

            if (big < root.val)
            {
                return LowestCommonAncestor(root.left, p, q);
            }
            if (small > root.val)
            {
                return LowestCommonAncestor(root.right, p, q);
            }
            return root;
        }
Ejemplo n.º 12
0
 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)) <= 1;
 }
Ejemplo n.º 13
0
 int Height(TreeNode root)
 {
     if (root == null) return 0;
     return Math.Max(Height(root.left), Height(root.right)) + 1;
 }
Ejemplo n.º 14
0
        public bool HasPathSum(TreeNode root, int sum)
        {
            if (root == null) return false;

            return (Dfs(root, sum));
        }
Ejemplo n.º 15
0
 public bool IsSymmetric(TreeNode root)
 {
     return root == null || IsSymmetric(root.left, root.right);
 }