private bool IsSymetricRecursion(TreeNode node1, TreeNode node2) { if (node1 == null && node2 == null) return true; else if (node1 == null || node2 == null) return false; if (node1.val != node2.val) return false; return IsSymetricRecursion(node1.left, node2.right) && IsSymetricRecursion(node1.right,node2.left); }
private int GetHeight(TreeNode root) { if (root == null) return 0; int leftNode = GetHeight(root.left); int rightNode = GetHeight(root.right); return Math.Max(leftNode, rightNode) + 1; }
public int MaxDepth(TreeNode root) { if (root == null) return 0; int leftNode = this.MaxDepth(root.left); int rightNode = this.MaxDepth(root.right); return Math.Max(leftNode, rightNode) + 1; }
private IList<int> PostorderTraversalByNoRecursiveAndStack(TreeNode root) { var list = new List<int>(); if (root == null) return list; var stack = new Stack<TreeNode>(); stack.Push(root); TreeNode pre = null, curr = null; while (stack.Count > 0 || curr == null) { curr = stack.Peek(); if ((curr.left == null && curr.right == null) || (pre != null && (pre == curr.left || pre == curr.right))) { list.Add(curr.val); stack.Pop(); pre = curr; } else { if (curr.right != null) stack.Push(curr.right); if (curr.left != null) stack.Push(curr.left); } } return list; }
//使用队列+深度优先遍历 private int MaxDepthByQueueAndDFS(TreeNode root) { if (root == null) return 0; Queue<TreeNode> queue = new Queue<TreeNode>(); queue.Enqueue(root); int count = 1, depth = 0; while (queue != null && queue.Count > 0) { TreeNode temp = queue.Peek(); queue.Dequeue(); count--; if (temp.left != null) queue.Enqueue(temp.left); if (temp.right != null) queue.Enqueue(temp.right); if (count == 0) { depth++; count = queue.Count; } } return depth; }
public bool IsSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; else if (p == null || q == null) return false; if (p.val != q.val) return false; return IsSameTree(p.left, q.left) && IsSameTree(p.right, q.right); }
//calculate height of right tree, if the same as height, go to right tree(append 1 to binary result), //otherwise go to left tree (Append 0 to binary result) private int CountNodes(TreeNode root) { if (root == null) return 0; TreeNode p = root, visit = root; int nodeCounts = 1, TreeHeight = 0; for (; p != null; p = p.left) { TreeHeight++; } while (--TreeHeight > 0) { nodeCounts <<= 1; p = visit.right; int RTreeHeight = 0; if (p != null) for (; p != null; p = p.left) RTreeHeight++; if (RTreeHeight < TreeHeight) { visit = visit.left; } else { nodeCounts |= 1; visit = visit.right; } } return nodeCounts; }
private TreeNode InvertBinaryTreeByRecursion(TreeNode root) { if (root == null) return root; TreeNode Node = InvertBinaryTreeByRecursion(root.left); root.left = InvertBinaryTreeByRecursion(root.right); root.right = Node; return root; }
private bool isValidHelper(TreeNode root, int? min, int? max) { if (root == null) return true; if (min != null && root.val <= min) return false; if (max != null && root.val >= max) return false; return isValidHelper(root.left, min, root.val) && isValidHelper(root.right, root.val, max); }
private bool IsSymmetricHelp(TreeNode left, TreeNode right) { if (left == null || right == null) return left == right; if (left.val != right.val) return false; return IsSymmetricHelp(left.left, right.right) && IsSymmetricHelp(left.right, right.left); }
public int MaxDepth(TreeNode root) { if (root == null) return 0; var leftDepth = 1 + MaxDepth(root.left); var rightDepth = 1 + MaxDepth(root.right); return Math.Max(leftDepth, rightDepth); }
private void Postorder(TreeNode root, IList<int> list) { if (root != null) { Postorder(root.left, list); Postorder(root.right, list); list.Add(root.val); } }
private void InOrder(IList<int> list, TreeNode node) //中序序遍历 { if (node != null) { InOrder(list, node.left); list.Add(node.val); InOrder(list, node.right); } }
public void AfterOrder(TreeNode node) //后继遍历 { if (node != null) { AfterOrder(node.left); //递归 AfterOrder(node.right); //递归 Console.Write(node.val); //打印字符 } }
public void MidOrder(TreeNode node) //中序遍历 { if (node != null) { MidOrder(node.left); //递归 Console.Write(node.val); //打印字符 MidOrder(node.right); //递归 } }
private void PreOrder(IList<int> list, TreeNode node) //先序遍历 { if (node != null) { list.Add(node.val); //打印字符 PreOrder(list, node.left); //递归 PreOrder(list, node.right); //递归 } }
private bool IsSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p != null && q != null && p.val == q.val) return IsSameTree(p.left, q.left) && IsSameTree(p.right, q.right); return false; }
public void PreOrder(TreeNode node) //先序遍历 { if (node != null) { Console.Write(node.val); //打印字符 PreOrder(node.left); //递归 PreOrder(node.right); //递归 } }
public int count;//用于记录节点个数 public BinaryTree(string constructStr) { cStr = constructStr; if (!string.IsNullOrEmpty(cStr)) { root = new TreeNode(cStr[0]); //添加头结点 Add(root, 0); //给头结点添加孩子结点 } }
public TreeNode InvertTree(TreeNode root) { if (root == null) return null; var left = root.left; var right = root.right; root.right = this.InvertTree(left); root.left = this.InvertTree(right); return root; }
public TreeNode InvertTree(TreeNode root) { if (root == null) return null; TreeNode right = InvertTree(root.right); TreeNode left = InvertTree(root.left); root.left = right; root.right = left; return root; }
public bool HasPathSum(TreeNode root, int sum) { if (root == null) return false; if(root.left ==null && root.right == null) { if (root.val == sum) return true; } return HasPathSum(root.left, sum - root.val) || HasPathSum(root.right, sum - root.val); }
public bool IsBalanced(TreeNode root) { if (root == null) return true; if(Math.Abs(GetHeight(root.left) - GetHeight(root.right)) <=1 ) { return IsBalanced(root.left) && IsBalanced(root.right); } return false; }
public int CountNodes(TreeNode node) //计算节点个数 { if (node != null) { CountNodes(node.left); //递归 CountNodes(node.right); //递归 count++; } return count; }
private Tuple<int, int> robHelper(TreeNode root) { if (root == null) return new Tuple<int, int>(0, 0); var leftMax = robHelper(root.left); var rightMax = robHelper(root.right); int robChildRoot = root.val + leftMax.Item2 + rightMax.Item2; int notRobChildRoot = Math.Max(leftMax.Item1, leftMax.Item2) + Math.Max(rightMax.Item1, rightMax.Item2); return new Tuple<int, int>(robChildRoot, notRobChildRoot); }
public bool IsSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) { return true; } else if (p != null && q != null && p.val == q.val) { return IsSameTree(p.left, q.left) && IsSameTree(p.right, q.right); } return false; }
private TreeNode InvertBinaryTreeByNoRecursion(TreeNode root) { if (root == null) return root; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.Push(root); while (stack.Count > 0) { TreeNode node = stack.Pop(); TreeNode temp = node.left; node.left = node.right; node.right = temp; if (node.left != null) stack.Push(node.left); if (node.right != null) stack.Push(node.right); } return root; }
private IList<int> PreorderTraversalByNoRecursive(TreeNode root) { IList<int> list = new List<int>(); Stack<TreeNode> stack = new Stack<TreeNode>(); while (root != null) { list.Add(root.val); if (root.right != null) stack.Push(root.right); root = root.left; if (root == null && stack.Count > 0) root = stack.Pop(); } return list; }
private IList<int> InOrderTraversalByNoRecursive(TreeNode root) { var list = new List<int>(); var stack = new Stack<TreeNode>(); var node = root; while (node != null || stack.Count > 0) { while (node != null) { stack.Push(node); node = node.left; } node = stack.Pop(); list.Add(node.val); node = node.right; } return list; }
public IList<int> PreorderTraversal(TreeNode root) { var list = new List<int>(); Stack<TreeNode> stack = new Stack<TreeNode>(); if(root != null) { stack.Push(root); } while(stack.Count != 0) { TreeNode currentNode = stack.Pop(); list.Add(currentNode.val); if (currentNode.right != null) stack.Push(currentNode.right); if (currentNode.left != null) stack.Push(currentNode.left); } return list; }