Exemplo n.º 1
0
        public void printAllPaths(TreeNode node, stack <TreeNode> treeStack)
        {
            if (node == null)
            {
                return;
            }

            treeStack.push(node);

            if (node.lchild == null && node.rchild == null)
            {
                treeStack.printStack();
                treeStack.pop();
                return;
            }

            if (node.lchild != null)
            {
                printAllPaths(node.lchild, treeStack);
            }

            if (node.rchild != null)
            {
                printAllPaths(node.rchild, treeStack);
            }

            treeStack.pop();
        }
Exemplo n.º 2
0
        //------Depth first tree traversal without recursion

        public void depthFirstTreeTraversal(TreeNode root)
        {
            stack <TreeNode> stk = new stack <TreeNode>();

            stk.push(root);
            while (!stk.isEmpty())
            {
                TreeNode tempNode = stk.pop();
                Console.WriteLine(tempNode.value);
                if (tempNode.lchild != null)
                {
                    stk.push(tempNode.lchild);
                }

                if (tempNode.rchild != null)
                {
                    stk.push(tempNode.rchild);
                }
            }
        }
Exemplo n.º 3
0
        public void initPrintAllPaths(TreeNode root)
        {
            stack <TreeNode> treeStack = new stack <TreeNode>();

            printAllPaths(root, treeStack);
        }