public int sumNumbers(TreeAndGraph.BinaryTreeNode root) { int count = 0; BFS(root, 0, ref count); return(count); }
private static void printSum(TreeAndGraph.BinaryTreeNode head, int level, double sum, double[] tab) { tab[level] = head.Data; double count = 0; for (int i = level; i >= 0; i--) { count += tab[i]; if (count == sum) { for (int j = level; j >= i; j--) { System.Console.Write(tab[j] + " "); } System.Console.WriteLine(); } } if (head.LeftNode != null) { printSum(head.LeftNode, level + 1, sum, tab); } if (head.RightNode != null) { printSum(head.RightNode, level + 1, sum, tab); } //unnecessary tab[level] = -1; }
public List <List <double> > levelOrderBottom(TreeAndGraph.BinaryTreeNode root) { List <List <double> > solution = new List <List <double> >(); DFS(solution, root, 0); return(solution); }
public List <double> inorder(TreeAndGraph.BinaryTreeNode root) { List <double> list = new List <double>(); Stack <TreeAndGraph.BinaryTreeNode> s = new Stack <BinaryTreeNode>(); s.Push(root); while (s.Count != 0) { while (s.Peek().LeftNode != null) { s.Push(s.Peek().LeftNode); } do { TreeAndGraph.BinaryTreeNode node = s.Pop(); list.Add(node.Data); if (node.RightNode != null) { s.Push(node.RightNode); break; } } while (s.Count != 0); } return(list); }
void PreDPS(TreeAndGraph.BinaryTreeNode node, List <List <double> > solution, int sum, double[] count, int level) { if (node == null) { return; } count[level] = node.Data; double temp = sum; for (int i = level; i >= 0; i--) { temp -= count[i]; if (temp == 0) { List <double> temp_list = new List <double>(); for (int j = i; j <= level; j++) { temp_list.Add(count[j]); } solution.Add(temp_list); } } PreDPS(node.LeftNode, solution, sum, count, level + 1); PreDPS(node.RightNode, solution, sum, count, level + 1); }
public static void mirrorTree(TreeAndGraph.BinaryTreeNode root) { if (root == null) { return; } if (root.LeftNode != null && root.RightNode != null) { TreeAndGraph.BinaryTreeNode temp = root.LeftNode; root.LeftNode = root.RightNode; root.RightNode = temp; mirrorTree(root.LeftNode); mirrorTree(root.RightNode); } else if (root.LeftNode != null) { root.LeftNode = root.RightNode; mirrorTree(root.LeftNode); } else if (root.RightNode != null) { root.RightNode = root.LeftNode; mirrorTree(root.RightNode); } else { return; } }
public int minDepth2(TreeAndGraph.BinaryTreeNode root) { Queue <TreeAndGraph.BinaryTreeNode> q = new Queue <BinaryTreeNode>(); q.Enqueue(root); root.state = 1; while (q.Count != 0) { BinaryTreeNode node = q.Dequeue(); if (node.LeftNode == null && node.RightNode == null) { return(node.state); } else { if (node.LeftNode != null) { node.LeftNode.state = node.state + 1; q.Enqueue(node.LeftNode); } if (node.RightNode != null) { node.RightNode.state = node.state + 1; q.Enqueue(node.RightNode); } } } return(-1); }
//less space complexity //using reused array instead of list public static void findSum5V(TreeAndGraph.BinaryTreeNode head, double sum) { int depth = getDepth(head); double[] tab = new double[depth]; printSum(head, 0, sum, tab); }
public static void interateBinaryTree(TreeAndGraph.BinaryTreeNode head, Order o) { if (head == null) { return; } if (o == Order.Pre) { System.Console.WriteLine("Pre:" + head.Data); } interateBinaryTree(head.LeftNode, o); if (o == Order.Mid) { System.Console.WriteLine("Mid:" + head.Data); } interateBinaryTree(head.RightNode, o); if (o == Order.Post) { System.Console.WriteLine("Post:" + head.Data); } }
private static int getDepth(TreeAndGraph.BinaryTreeNode head) { if (head == null) { return(0); } return(System.Math.Max(getDepth(head.LeftNode), getDepth(head.RightNode)) + 1); }
public int maxDepth2(TreeAndGraph.BinaryTreeNode root) { if (root == null) { return(0); } return(Math.Max(maxDepth2(root.LeftNode), maxDepth2(root.RightNode)) + (int)root.Data); }
public double minDepth(TreeAndGraph.BinaryTreeNode root) { if (root == null) { return(0); } return(Math.Min(minDepth(root.LeftNode), minDepth(root.RightNode)) + root.Data); }
public List <List <double> > pathSum(TreeAndGraph.BinaryTreeNode root, int sum) { List <List <double> > solution = new List <List <double> >(); int height = root.getHeight(); count = new double[height]; PreDPS(root, solution, sum, count, 0); return(solution); }
public static int countMaxDistanceHelper(TreeAndGraph.BinaryTreeNode root) { if (root == null) { return(0); } return(1 + System.Math.Abs(countMaxDistanceHelper(root.LeftNode) - countMaxDistanceHelper(root.RightNode))); }
public static TreeAndGraph.BinaryTreeNode findCommonAncesterV5(TreeAndGraph.BinaryTreeNode root, TreeAndGraph.BinaryTreeNode p, TreeAndGraph.BinaryTreeNode q) { Result r = findCommonAncesterWrap(root, p, q); if (r.find) { return(r.btn); } return(null); }
public bool validate(TreeAndGraph.BinaryTreeNode root, double min = double.MinValue, double max = double.MaxValue) { if (root == null) { return(true); } if (root.Data < min || root.Data > max) { return(false); } return(validate(root.LeftNode, min, root.Data) && validate(root.RightNode, root.Data, max)); }
public bool verify(TreeAndGraph.BinaryTreeNode t1, TreeAndGraph.BinaryTreeNode t2) { if (t1 == null && t2 == null) { return(true); } else if (t1 == null || t2 == null || t1.Data != t2.Data) { return(false); } return(verify(t1.LeftNode, t2.LeftNode) && verify(t1.RightNode, t2.RightNode)); }
bool DFS(TreeAndGraph.BinaryTreeNode left, TreeAndGraph.BinaryTreeNode right) { if (left == null && right == null) { return(true); } else if (left == null || right == null || left.Data != right.Data) { return(false); } return(DFS(left.LeftNode, right.RightNode) && DFS(left.RightNode, right.LeftNode)); }
public void recover(TreeAndGraph.BinaryTreeNode root) { last = null; forward(root); last = null; backward(root); if (f != b) { double temp = f.Data; f.Data = b.Data; b.Data = temp; } }
private static void PutArr(TreeAndGraph.BinaryTreeNode root, double[] arr) { if (root == null) { return; } PutArr(root.LeftNode, arr); arr[index++] = root.Data; PutArr(root.RightNode, arr); }
//revision: clear 12/1/2012 /* * TreeAndGraph.BinaryTreeNode btn10 = new TreeAndGraph.BinaryTreeNode(10); * TreeAndGraph.BinaryTreeNode btn6 = new TreeAndGraph.BinaryTreeNode(6); * TreeAndGraph.BinaryTreeNode btn14 = new TreeAndGraph.BinaryTreeNode(14); * TreeAndGraph.BinaryTreeNode btn4 = new TreeAndGraph.BinaryTreeNode(4); * TreeAndGraph.BinaryTreeNode btn8 = new TreeAndGraph.BinaryTreeNode(8); * TreeAndGraph.BinaryTreeNode btn12 = new TreeAndGraph.BinaryTreeNode(12); * TreeAndGraph.BinaryTreeNode btn16 = new TreeAndGraph.BinaryTreeNode(16); * btn10.LeftNode = btn6; btn10.RightNode = btn14; btn6.LeftNode = btn4; btn6.RightNode = btn8; btn14.LeftNode = btn12; btn14.RightNode = btn16; * swapTree(btn10); */ public static void swapTree(TreeAndGraph.BinaryTreeNode node) { if (node == null) { return; } TreeAndGraph.BinaryTreeNode temp = node.LeftNode; node.LeftNode = node.RightNode; node.RightNode = temp; swapTree(node.LeftNode); swapTree(node.RightNode); }
public static BinaryTreeNode getBSTInst() { TreeAndGraph.BinaryTreeNode btn1 = new TreeAndGraph.BinaryTreeNode(10); TreeAndGraph.BinaryTreeNode btn2 = new TreeAndGraph.BinaryTreeNode(5); TreeAndGraph.BinaryTreeNode btn3 = new TreeAndGraph.BinaryTreeNode(15); TreeAndGraph.BinaryTreeNode btn4 = new TreeAndGraph.BinaryTreeNode(2); TreeAndGraph.BinaryTreeNode btn5 = new TreeAndGraph.BinaryTreeNode(7); TreeAndGraph.BinaryTreeNode btn6 = new TreeAndGraph.BinaryTreeNode(12); TreeAndGraph.BinaryTreeNode btn7 = new TreeAndGraph.BinaryTreeNode(17); btn1.LeftNode = btn2; btn1.RightNode = btn3; btn2.LeftNode = btn4; btn2.RightNode = btn5; btn3.LeftNode = btn6; btn3.RightNode = btn7; return(btn1); }
void DFS(List <List <double> > solution, TreeAndGraph.BinaryTreeNode root, int level) { if (root == null) { return; } if (solution.Count <= level) { solution.Insert(0, new List <double>()); } solution[solution.Count - 1 - level].Add(root.Data); DFS(solution, root.LeftNode, level + 1); DFS(solution, root.RightNode, level + 1); }
public static BinaryTreeNode getSymInst() { TreeAndGraph.BinaryTreeNode btn1 = new TreeAndGraph.BinaryTreeNode(1); TreeAndGraph.BinaryTreeNode btn2 = new TreeAndGraph.BinaryTreeNode(2); TreeAndGraph.BinaryTreeNode btn3 = new TreeAndGraph.BinaryTreeNode(2); TreeAndGraph.BinaryTreeNode btn4 = new TreeAndGraph.BinaryTreeNode(4); TreeAndGraph.BinaryTreeNode btn5 = new TreeAndGraph.BinaryTreeNode(5); TreeAndGraph.BinaryTreeNode btn6 = new TreeAndGraph.BinaryTreeNode(5); TreeAndGraph.BinaryTreeNode btn7 = new TreeAndGraph.BinaryTreeNode(4); btn1.LeftNode = btn2; btn1.RightNode = btn3; btn2.LeftNode = btn4; btn2.RightNode = btn5; btn3.LeftNode = btn6; btn3.RightNode = btn7; return btn1; }
private Wrapper DFS(TreeAndGraph.BinaryTreeNode head) { Wrapper wr = new Wrapper(); if (head == null) { return(wr); } Wrapper l = DFS(head.LeftNode); Wrapper r = DFS(head.RightNode); wr.BranchLen = Math.Max(l.BranchLen, r.BranchLen) + (int)head.Data; wr.LoopLen = Math.Max(Math.Max(l.LoopLen, r.LoopLen), l.BranchLen + r.BranchLen + (int)head.Data); return(wr); }
//reference to Q39(Similar) public static int getLongestDistance(TreeAndGraph.BinaryTreeNode root, TreeAndGraph.BinaryTreeNode left, TreeAndGraph.BinaryTreeNode right) { ReturnWrapper wr = getLongestDistanceHelper(root, left, right); //left or right is root will done separately if (wr.findleft && wr.findright) { return(wr.count); } else { return(-1); } }
private void BFS(TreeAndGraph.BinaryTreeNode node, int cur, ref int count) { if (node == null) { return; } cur = cur * 10 + (int)node.Data; BFS(node.LeftNode, cur, ref count); BFS(node.RightNode, cur, ref count); //cur = (cur - (int)node.Data) / 10; if (node.LeftNode == null && node.RightNode == null) { count += cur; } }
public void connect(TreeAndGraph.BinaryTreeNode root) { TreeAndGraph.BinaryTreeNode cur_loop = root, down_node = root; while (down_node != null) { TreeAndGraph.BinaryTreeNode last_node = null; cur_loop = down_node; down_node = null; while (cur_loop != null) { if (cur_loop.LeftNode != null) { if (down_node == null) { down_node = cur_loop.LeftNode; } if (last_node == null) { last_node = cur_loop.LeftNode; } else { last_node.NextNode = cur_loop.LeftNode; last_node = cur_loop.LeftNode; } } if (cur_loop.RightNode != null) { if (down_node == null) { down_node = cur_loop.RightNode; } if (last_node == null) { last_node = cur_loop.RightNode; } else { last_node.NextNode = cur_loop.RightNode; last_node = cur_loop.RightNode; } } cur_loop = cur_loop.NextNode; } } }
public static void getDoubleLinkedListFromBinarySearchTreeInPlace(TreeAndGraph.BinaryTreeNode head) { if (head == null) { return; } getDoubleLinkedListFromBinarySearchTreeInPlace(head.LeftNode); if (last != null) { last.RightNode = head; } head.ParentNode = last; last = head; getDoubleLinkedListFromBinarySearchTreeInPlace(head.RightNode); }
public static void getDoubleLinkedListFromBinarySearchTreeInPlace(TreeAndGraph.BinaryTreeNode head) { if (head == null) { return; } getDoubleLinkedListFromBinarySearchTreeInPlace(head.LeftNode); if (last!=null) { last.RightNode = head; } head.ParentNode = last; last = head; getDoubleLinkedListFromBinarySearchTreeInPlace(head.RightNode); }
public static void BreathFirstSearch(TreeAndGraph.BinaryTreeNode root) { System.Collections.Queue q = new System.Collections.Queue(); q.Enqueue(root); while (q.Count != 0) { TreeAndGraph.BinaryTreeNode btn = (TreeAndGraph.BinaryTreeNode)q.Dequeue(); System.Console.WriteLine(btn.Data); if (btn.LeftNode != null) { q.Enqueue(btn.LeftNode); } if (btn.RightNode != null) { q.Enqueue(btn.RightNode); } } }
private void PreDFS(TreeAndGraph.BinaryTreeNode node) { if (node == null) { return; } TreeAndGraph.BinaryTreeNode l = node.LeftNode, r = node.RightNode; node.LeftNode = null; if (_last_node == null) { _last_node = node; } else { _last_node.RightNode = node; _last_node = node; } PreDFS(l); PreDFS(r); }
void backward(TreeAndGraph.BinaryTreeNode node) { if (node == null) { return; } backward(node.RightNode); if (last == null) { last = node; } else { if (node.Data > last.Data) { b = last; return; } last = node; } backward(node.LeftNode); }
void forward(TreeAndGraph.BinaryTreeNode node) { if (node == null) { return; } forward(node.LeftNode); if (last == null) { last = node; } else { if (node.Data < last.Data) { f = last; return; } last = node; } forward(node.RightNode); }
//reference to CC4.3 public static TreeAndGraph.BinaryTreeNode buildBinarySearchTree(int[] arr, int s = -1, int e = -1) { if (s == -1) { s = 0; } if (e == -1) { e = arr.Length - 1; } int m = (s + e) / 2; TreeAndGraph.BinaryTreeNode head = new TreeAndGraph.BinaryTreeNode(arr[m]); if (m - 1 >= s) { head.LeftNode = buildBinarySearchTree(arr, s, m - 1); } if (e >= m + 1) { head.RightNode = buildBinarySearchTree(arr, m + 1, e); } return head; }
public void flatten(TreeAndGraph.BinaryTreeNode root) { _last_node = null; PreDFS(root); }
public Result(bool find, TreeAndGraph.BinaryTreeNode btn) { this.find = find; this.btn = btn; }