public static void FindSum(TreeNode node, int sum, int[] path, int level) { if (node == null) { return; } /* Insert current node into path */ path[level] = node.Data; var t = 0; for (var i = level; i >= 0; i--) { t += path[i]; if (t == sum) { Print(path, i, level); } } FindSum(node.Left, sum, path, level + 1); FindSum(node.Right, sum, path, level + 1); /* Remove current node from path. Not strictly necessary, since we would * ignore this value, but it's good practice. */ path[level] = Int32.MinValue; }
public void SetLeftChild(TreeNode left) { this.Left = left; if (left != null) { left.Parent = this; } }
public void SetRightChild(TreeNode right) { this.Right = right; if (right != null) { right.Parent = this; } }
public static TreeNode InorderSucc(TreeNode node) { if (node == null) { return null; } // Found right children -> return left most node of right subtree if (node.Parent == null || node.Right != null) { return LeftMostChild(node.Right); } var q = node; var x = q.Parent; // Go up until we’re on left instead of right while (x != null && x.Left != q) { q = x; x = x.Parent; } return x; }
int CheckHeight(TreeNode root) { if (root == null) { return 0; } int leftHeight = CheckHeight(root.Left); if (leftHeight == -1) { return -1; } int rightHeight = CheckHeight(root.Right); if (rightHeight == -1) { return -1; } int heightDiff = leftHeight - rightHeight; if (Math.Abs(heightDiff) > 1) { return -1; } else { return Math.Max(leftHeight, rightHeight) + 1; } }
public static void FindSum(TreeNode node, int sum) { var depth = Depth(node); var path = new int[depth]; FindSum(node, sum, path, 0); }
int GetHeight(TreeNode root) { if (root == null) { return 0; } return Math.Max(GetHeight(root.Left), GetHeight(root.Right)) + 1; }
public static bool ContainsTree(TreeNode t1, TreeNode t2) { if (t2 == null) { return true; // The empty tree is a subtree of every tree. } return SubTree(t1, t2); }
public static int Depth(TreeNode node) { if (node == null) { return 0; } return 1 + Math.Max(Depth(node.Left), Depth(node.Right)); }
bool IsBalancedImproved(TreeNode root) { if (CheckHeight(root) == -1) { return false; } else { return true; } }
public void Run() { var root = new TreeNode(5); root.Left = new TreeNode(3); root.Right = new TreeNode(1); root.Left.Left = new TreeNode(4); root.Left.Right = new TreeNode(8); root.Right.Left = new TreeNode(2); root.Right.Right = new TreeNode(6); FindSum(root, 8); }
public static TreeNode LeftMostChild(TreeNode node) { if (node == null) { return null; } while (node.Left != null) { node = node.Left; } return node; }
bool IsBalanced(TreeNode root) { if (root == null) { return true; } int heightDiff = GetHeight(root.Left) - GetHeight(root.Right); if (Math.Abs(heightDiff) > 1) { return false; } else { return IsBalanced(root.Left) && IsBalanced(root.Right); } }
/* Checks if the binary tree rooted at node1 contains the binary tree * rooted at node2 as a subtree somewhere within it. */ public static bool SubTree(TreeNode node1, TreeNode node2) { if (node1 == null) { // big tree empty & subtree still not found. return false; } if (node1.Data == node2.Data) { if (MatchTree(node1, node2)) { return true; } } return (SubTree(node1.Left, node2) || SubTree(node1.Right, node2)); }
/* Creates tree by mapping the array left to right, top to bottom. */ public static TreeNode CreateTreeFromArray(int[] array) { if (array.Length > 0) { var root = new TreeNode(array[0]); var queue = new Queue<TreeNode>(); queue.Enqueue(root); var done = false; var i = 1; while (!done) { var treeNode = queue.Peek(); if (treeNode.Left == null) { treeNode.Left = new TreeNode(array[i]); i++; queue.Enqueue(treeNode.Left); } else if (treeNode.Right == null) { treeNode.Right = new TreeNode(array[i]); i++; queue.Enqueue(treeNode.Right); } else { queue.Dequeue(); } if (i == array.Length) { done = true; } } return root; } return null; }
public void Run() { // Create balanced tree int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; var root = TreeNode.CreateMinimalBst(array); Console.WriteLine("Root? " + root.Data); Console.WriteLine("Is balanced? " + IsBalanced(root)); Console.WriteLine("Improved Is balanced? " + IsBalancedImproved(root)); // Could be balanced, actually, but it's very unlikely... var unbalanced = new TreeNode(10); for (var i = 0; i < 10; i++) { unbalanced.InsertInOrder(AssortedMethods.RandomIntInRange(0, 100)); } Console.WriteLine("Root? " + unbalanced.Data); Console.WriteLine("Is balanced? " + IsBalanced(unbalanced)); Console.WriteLine("Improved Is balanced? " + IsBalancedImproved(unbalanced)); }
/* Checks if the binary tree rooted at node1 contains the * binary tree rooted at node2 as a subtree starting at node1. */ public static bool MatchTree(TreeNode node1, TreeNode node2) { if (node2 == null && node1 == null) { // nothing left in the subtree return true; } if (node1 == null || node2 == null) { // big tree empty & subtree still not found return false; } if (node1.Data != node2.Data) { // data doesn’t match return false; } return (MatchTree(node1.Left, node2.Left) && MatchTree(node1.Right, node2.Right)); }
// Checks how many “special” nodes are located under this root public static int Covers(TreeNode root, TreeNode p, TreeNode q) { var nodesFound = NoNodesFound; if (root == null) { return nodesFound; } if (root == p || root == q) { nodesFound += 1; } nodesFound += Covers(root.Left, p, q); if (nodesFound == TwoNodesFound) // Found p and q { return nodesFound; } return nodesFound + Covers(root.Right, p, q); }
public static List<LinkedList<TreeNode>> CreateLevelLinkedList(TreeNode root) { var result = new List<LinkedList<TreeNode>>(); /* "Visit" the root */ var current = new LinkedList<TreeNode>(); if (root != null) { current.AddFirst(root); } while (current.Count > 0) { // Add previous level result.Add(current); // Go to next level var parents = current; current = new LinkedList<TreeNode>(); foreach (var parent in parents) { /* Visit the children */ if (parent.Left != null) { current.AddLast(parent.Left); } if (parent.Right != null) { current.AddLast(parent.Right); } } } return result; }
private static TreeNode CreateMinimalBst(int[] arr, int start, int end) { if (end < start) { return null; } int mid = (start + end) / 2; TreeNode n = new TreeNode(arr[mid]); n.SetLeftChild(CreateMinimalBst(arr, start, mid - 1)); n.SetRightChild(CreateMinimalBst(arr, mid + 1, end)); return n; }
/* Creates tree by mapping the array left to right, top to bottom. */ public static TreeNode CreateTreeFromArray(int[] array) { if (array.Length > 0) { TreeNode root = new TreeNode(array[0]); Queue<TreeNode> queue = new Queue<TreeNode>(); queue.Enqueue(root); bool done = false; int i = 1 ; while (!done) { TreeNode r = (TreeNode) queue.Peek(); if (r.Left == null) { r.Left = new TreeNode(array[i]); i++; queue.Enqueue(r.Left); } else if (r.Right == null) { r.Right = new TreeNode(array[i]); i++; queue.Enqueue(r.Right); } else { queue.Dequeue(); } if (i == array.Length) done = true; } return root; } else { return null; } }
public static TreeNode RandomBst(int N, int min, int max) { int d = RandomIntInRange(min, max); TreeNode root = new TreeNode(d); for (int i = 1; i < N; i++) { root.InsertInOrder(RandomIntInRange(min, max)); } return root; }
public static TreeNode CommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (q == p && (root.Left == q || root.Right == q)) { return root; } // Check left side var nodesFromLeft = Covers(root.Left, p, q); if (nodesFromLeft == TwoNodesFound) { if (root.Left == p || root.Left == q) { return root.Left; } return CommonAncestor(root.Left, p, q); } else if (nodesFromLeft == OneNodeFound) { if (root == p) { return p; } else if (root == q) { return q; } } // Check right side var nodesFromRight = Covers(root.Right, p, q); if (nodesFromRight == TwoNodesFound) { if (root.Right == p || root.Right == q) { return root.Right; } return CommonAncestor(root.Right, p, q); } else if (nodesFromRight == OneNodeFound) { if (root == p) { return p; } else if (root == q) { return q; } } if (nodesFromLeft == OneNodeFound && nodesFromRight == OneNodeFound) { return root; } return null; }
private static TreeNode CreateMinimalBst(int[] array, int start, int end) { if (end < start) { return null; } var mid = (start + end) / 2; var treeNode = new TreeNode(array[mid]); treeNode.SetLeftChild(CreateMinimalBst(array, start, mid - 1)); treeNode.SetRightChild(CreateMinimalBst(array, mid + 1, end)); return treeNode; }