static void Main(string[] args) { ListNode l1 = new ListNode(1); ListNode two = new ListNode(3); ListNode three = new ListNode(6); ListNode four = new ListNode(7); ListNode l2 = new ListNode(2); ListNode six = new ListNode(4); ListNode seven = new ListNode(6); ListNode eight = new ListNode(8); l1.next = two; two.next = three; three.next = four; l2.next = six; six.next = seven; seven.next = eight; TreeNode t1 = new TreeNode(1); TreeNode t2 = new TreeNode(2); TreeNode t3 = new TreeNode(3); TreeNode t4 = new TreeNode(1); TreeNode t5 = new TreeNode(3); //TreeNode t6 = new TreeNode(6); //7TreeNode t7 = new TreeNode(7); //TreeNode t8 = new TreeNode(8); t1.left = t2; t1.right = t3; t2.left = t4; t2.right = t5; //t4.left = t7; //t3.right = t6; //t6.right = t8; IList < IList < int >> test = Submissions.Generate(4); Console.Write(Submissions.HasPathSum(t1, 6)); Console.In.ReadLine(); }
public static int MinDepth(TreeNode root) { if (root == null) { return 0; } else if (root.left == null && root.right == null) { return 1; } else { return Math.Min(root.left != null ? int.MaxValue + MinDepth(root.left) : int.MaxValue, root.right != null ? 1 + MinDepth(root.right) : int.MaxValue); } }
public static int MaxDepth(TreeNode root) { if (root == null) return 0; else if (root.left == null && root.right == null) return 1; else return System.Math.Max(MaxDepth(root.right) + 1, MaxDepth(root.left) + 1); }
public static TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return null; if (root.val >= p.val && root.val <= q.val) return root; if (root.val < p.val && root.val < q.val) return LowestCommonAncestor(root.right, p, q); if (root.val > p.val && root.val > q.val) return LowestCommonAncestor(root.left, p, q); return root; }
public static bool IsSymmetric(TreeNode root) { if (root == null) { return true; } if (IsSameTree(InvertTree(root.left), root.right)) { return true; } else { return false; } }
public static bool IsSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if ((p == null && q != null) || (p != null && q == null)) { return false; } if (p.val != q.val) return false; return (IsSameTree(p.left, q.left) && IsSameTree(p.right, q.right)); }
public static bool IsBalanced(TreeNode root) { if (root == null) { return true; } if (Math.Abs(MaxDepth(root.left) - MaxDepth(root.right)) > 1) { return false; } return IsBalanced(root.left) && IsBalanced(root.right); }
public static void InvertTreeFromTopNode(TreeNode root) { if (root == null) return; TreeNode temp = root.left; root.left = root.right; root.right = temp; InvertTreeFromTopNode(root.left); InvertTreeFromTopNode(root.right); }
public static TreeNode InvertTree(TreeNode root) { InvertTreeFromTopNode(root); return root; }
public static bool HasPathSum(TreeNode root, int sum) { if (root == null) { return false; } if (sum - root.val == 0 && root.left == null && root.right == null) { return true; } if (sum - root.val != 0 && root.left == null && root.right == null) { return false; } return (root.left != null) ? HasPathSum(root.left, sum - root.val) : false || (root.right != null) ? HasPathSum(root.right, sum - root.val) : false; }