public static void Process(TreeNode root) { Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode current = root; TreeNode temp; while (current != null || stack.Count > 0) { while (current != null) { stack.Push(current); temp = current.Left; current.Left = current.Right; current.Right = temp; current = current.Left; } if (stack.Count > 0) { current = stack.Pop().Right; } } }
public void TestFirstIsNull() { TreeNode first = null; TreeNode second = new TreeNode(1); Assert.IsFalse(SubstructureInTree.Process(first, second)); }
public static void Process(TreeNode root, int sum) { if (root == null) { return; } }
public void TestInOrderTraverseOnlyRootTree() { TreeNode root = new TreeNode(1); string expected = "1"; TreeNode.InOrderTraverseRecursively(root); Assert.AreEqual(expected, TreeNode.Result); TreeNode.InOrderTraverseIteratively(root); Assert.AreEqual(expected, TreeNode.Result); }
public static TreeNode ProcessIteratively(int[] preOrder, int[] inOrder) { if (preOrder == null || inOrder == null || preOrder.Length == 0 || inOrder.Length == 0 || preOrder.Length != inOrder.Length) { return null; } Dictionary<int, int> orders = new Dictionary<int, int>(); for(int i = 0; i < inOrder.Length; ++i) { orders.Add(inOrder[i], i); } TreeNode root = new TreeNode(preOrder[0]); Stack<TreeNode> stack = new Stack<TreeNode>(); stack.Push(root); TreeNode current = null; TreeNode previous = null; for (int i = 1; i < preOrder.Length; ++i) { current = new TreeNode(preOrder[i]); if (!orders.ContainsKey(current.Value)) { throw new ArgumentException(Constants.InvalidInput); } if (orders[current.Value] < orders[stack.Peek().Value]) { stack.Peek().Left = current; stack.Push(current); } else { while (stack.Count > 0 && orders[current.Value] > orders[stack.Peek().Value]) { previous = stack.Pop(); } previous.Right = current; stack.Push(current); } } return root; }
public static TreeNode Clone(TreeNode root) { if (root == null) { return null; } TreeNode cloned = new TreeNode(root.Value); cloned.Left = Clone(root.Left); cloned.Right = Clone(root.Right); return cloned; }
public void TestNotSubTreeInNoRightChildTree() { TreeNode first = new TreeNode(8); first.Left = new TreeNode(8); first.Left.Left = new TreeNode(9); first.Left.Left.Left = new TreeNode(3); first.Left.Left.Left.Left = new TreeNode(5); TreeNode second = new TreeNode(8); second.Left = new TreeNode(9); second.Left.Left = new TreeNode(2); Assert.IsFalse(SubstructureInTree.Process(first, second)); }
public void TestCompleteTree() { TreeNode root = Helper.CreateTree(TreeType.CompleteTree); TreeNode expected = new TreeNode(1); expected.Left = new TreeNode(3); expected.Left.Left = new TreeNode(7); expected.Left.Right = new TreeNode(6); expected.Right = new TreeNode(2); expected.Right.Left = new TreeNode(5); expected.Right.Right = new TreeNode(4); MirrorOfBinaryTree.Process(root); Assert.IsTrue(Helper.CompareBinaryTree(expected, root)); root = Helper.CreateTree(TreeType.CompleteTree); MirrorOfBinaryTree.ProcessRecursively(root); Assert.IsTrue(Helper.CompareBinaryTree(expected, root)); }
public static bool CompareBinaryTree(TreeNode first, TreeNode second) { if (first == second) { return true; } if (first == null || second == null) { return false; } if (!first.Equals(second)) { return false; } return CompareBinaryTree(first.Left, second.Left) && CompareBinaryTree(first.Right, second.Right); }
public static void InOrderTraverseIteratively(TreeNode root) { Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode current = root; while (current != null || stack.Count > 0) { while (current != null) { stack.Push(current); current = current.Left; } if (stack.Count > 0) { current = stack.Pop(); output.Append(current.Value); current = current.Right; } } }
public static void ProcessRecursively(TreeNode root) { if (root == null) { return; } TreeNode temp = root.Left; root.Left = root.Right; root.Right = temp; if (root.Left != null) { ProcessRecursively(root.Left); } if (root.Right != null) { ProcessRecursively(root.Right); } }
public void TestLevelOrderTraverseOnlyRootTree() { TreeNode root = new TreeNode(1); string expected = "1"; TreeNode.LevelOrderTraverse(root); Assert.AreEqual(expected, TreeNode.Result); }
public void TestOnlyOneNode() { TreeNode root = new TreeNode(1); TreeNode expected = new TreeNode(1); MirrorOfBinaryTree.Process(root); Assert.IsTrue(Helper.CompareBinaryTree(expected, root)); root = new TreeNode(1); MirrorOfBinaryTree.ProcessRecursively(root); Assert.IsTrue(Helper.CompareBinaryTree(expected, root)); }
public static void InOrderTraverseRecursively(TreeNode root) { if (root != null) { InOrderTraverseRecursively(root.Left); output.Append(root.Value); InOrderTraverseRecursively(root.Right); } }
public static void PostOrderTraverseIteratively(TreeNode root) { Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode current = root; TreeNode previous = null; while (current != null || stack.Count > 0) { while (current != null) { stack.Push(current); current = current.Left; } current = stack.Peek(); if (current.Right == null || current.Right == previous) { output.Append(current.Value); previous = current; stack.Pop(); current = null; } else { current = current.Right; } } }
public void TestOnlyRootTree() { int[] preOrder = new int[] { 1 }; int[] inOrder = new int[] { 1 }; TreeNode expected = new TreeNode(1); Assert.IsTrue(Helper.CompareBinaryTree(expected, ConstructBinaryTree.ProcessRecursively(preOrder, inOrder))); Assert.IsTrue(Helper.CompareBinaryTree(expected, ConstructBinaryTree.ProcessIteratively(preOrder, inOrder))); }
private static TreeNode ProcessCore( int[] preOrder, int startPreOrder, int endPreOrder, int[] inOrder, int startInOrder, int endInOrder) { TreeNode root = new TreeNode(preOrder[startPreOrder]); if (startPreOrder == endPreOrder) { if (startInOrder == endInOrder && preOrder[startPreOrder] == inOrder[startInOrder]) { return root; } else { throw new ArgumentException(Constants.InvalidInput); } } int rootInOrder = startInOrder; while (rootInOrder <= endInOrder && inOrder[rootInOrder] != root.Value) { ++rootInOrder; } if (rootInOrder == endInOrder && inOrder[rootInOrder] != root.Value) { throw new ArgumentException(Constants.InvalidInput); } int leftLength = rootInOrder - startInOrder; int leftPreOrderEnd = startPreOrder + leftLength; if (leftLength > 0) { root.Left = ProcessCore( preOrder, startPreOrder + 1, leftPreOrderEnd, inOrder, startInOrder, rootInOrder - 1); } if (leftLength < endPreOrder - startPreOrder) { root.Right = ProcessCore( preOrder, leftPreOrderEnd + 1, endPreOrder, inOrder, rootInOrder + 1, endInOrder); } return root; }
private static TreeNode CreateNoRightChildTree() { TreeNode root = new TreeNode(1); root.Left = new TreeNode(2); root.Left.Left = new TreeNode(3); root.Left.Left.Left = new TreeNode(4); root.Left.Left.Left.Left = new TreeNode(5); return root; }
public void TestWithSameTree() { TreeNode first = new TreeNode(8); first.Left = new TreeNode(8); first.Left.Left = new TreeNode(9); first.Left.Right = new TreeNode(2); first.Left.Right.Left = new TreeNode(4); first.Left.Right.Right = new TreeNode(7); first.Right = new TreeNode(7); TreeNode second = Helper.Clone(first); Assert.IsTrue(SubstructureInTree.Process(first, second)); }
public void TestSubTreeInNotCompleteTree() { TreeNode first = new TreeNode(8); first.Left = new TreeNode(8); first.Left.Left = new TreeNode(9); first.Left.Right = new TreeNode(2); first.Left.Right.Left = new TreeNode(4); first.Left.Right.Right = new TreeNode(7); first.Right = new TreeNode(7); TreeNode second = new TreeNode(8); second.Left = new TreeNode(9); second.Right = new TreeNode(2); Assert.IsTrue(SubstructureInTree.Process(first, second)); }
public void TestSubTreeInNoLeftChildTree() { TreeNode first = new TreeNode(8); first.Right = new TreeNode(8); first.Right.Right = new TreeNode(9); first.Right.Right.Right = new TreeNode(2); first.Right.Right.Right.Right = new TreeNode(5); TreeNode second = new TreeNode(8); second.Right = new TreeNode(9); second.Right.Right = new TreeNode(2); Assert.IsTrue(SubstructureInTree.Process(first, second)); }
public static void LevelOrderTraverse(TreeNode root) { if (root == null) { return; } Queue<TreeNode> queue = new Queue<TreeNode>(); queue.Enqueue(root); TreeNode current; while (queue.Count > 0) { current = queue.Dequeue(); output.Append(current.Value); if (current.Left != null) { queue.Enqueue(current.Left); } if (current.Right != null) { queue.Enqueue(current.Right); } } }
private static TreeNode CreateNotCompleteTree() { TreeNode root = new TreeNode(1); root.Left = new TreeNode(2); root.Left.Left = new TreeNode(4); root.Left.Left.Right = new TreeNode(7); root.Right = new TreeNode(3); root.Right.Left = new TreeNode(5); root.Right.Right = new TreeNode(6); root.Right.Right.Left = new TreeNode(8); return root; }
public void TestOnlyOneNode() { TreeNode first = new TreeNode(1); TreeNode second = new TreeNode(1); Assert.IsTrue(SubstructureInTree.Process(first, second)); }
public IList <int> PreorderTraversal(TreeNode root) { return(PreorderTraversalIterative(root)); }