예제 #1
0
        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;
                }
            }
        }
예제 #2
0
        public void TestFirstIsNull()
        {
            TreeNode first = null;
            TreeNode second = new TreeNode(1);

            Assert.IsFalse(SubstructureInTree.Process(first, second));
        }
예제 #3
0
 public static void Process(TreeNode root, int sum)
 {
     if (root == null)
     {
         return;
     }
 }
예제 #4
0
        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);
        }
예제 #5
0
        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;
        }
예제 #6
0
파일: Helper.cs 프로젝트: eriolchan/problem
        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;
        }
예제 #7
0
        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));
        }
예제 #8
0
        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));
        }
예제 #9
0
파일: Helper.cs 프로젝트: eriolchan/problem
        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);
        }
예제 #10
0
        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;
                }
            }
        }
예제 #11
0
        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);
            }
        }
예제 #12
0
        public void TestLevelOrderTraverseOnlyRootTree()
        {
            TreeNode root = new TreeNode(1);
            string expected = "1";

            TreeNode.LevelOrderTraverse(root);
            Assert.AreEqual(expected, TreeNode.Result);
        }
예제 #13
0
        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));
        }
예제 #14
0
 public static void InOrderTraverseRecursively(TreeNode root)
 {
     if (root != null)
     {
         InOrderTraverseRecursively(root.Left);
         output.Append(root.Value);
         InOrderTraverseRecursively(root.Right);
     }
 }
예제 #15
0
        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;
                }
            }
        }
예제 #16
0
        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)));
        }
예제 #17
0
        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;
        }
예제 #18
0
파일: Helper.cs 프로젝트: eriolchan/problem
        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;
        }
예제 #19
0
        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));
        }
예제 #20
0
        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));
        }
예제 #21
0
        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));
        }
예제 #22
0
        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);
                }
            }
        }
예제 #23
0
파일: Helper.cs 프로젝트: eriolchan/problem
        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;
        }
예제 #24
0
        public void TestOnlyOneNode()
        {
            TreeNode first = new TreeNode(1);
            TreeNode second = new TreeNode(1);

            Assert.IsTrue(SubstructureInTree.Process(first, second));
        }
예제 #25
0
 public IList <int> PreorderTraversal(TreeNode root)
 {
     return(PreorderTraversalIterative(root));
 }