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; }
public int MaxDepth(TreeNode root) { if (root == null) { return 0; } return 1+ Math.Max(MaxDepth(root.left), MaxDepth(root.right)); }
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; }
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); }
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(); }
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); }
// 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; }
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!"); }
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); }
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; }
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; }
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; }
int Height(TreeNode root) { if (root == null) return 0; return Math.Max(Height(root.left), Height(root.right)) + 1; }
public bool HasPathSum(TreeNode root, int sum) { if (root == null) return false; return (Dfs(root, sum)); }
public bool IsSymmetric(TreeNode root) { return root == null || IsSymmetric(root.left, root.right); }