The Java implementation of the pre-order, middle order and post-sequence traversal of the tree

Source: Internet
Author: User

"Note" Subsequent implementation of the second method is temporarily not solved

1  PackageCom.exe4.offer;2 3 ImportJava.util.Stack;4 5 /**6 * Pre-order, middle-order, post-sequence traversal methods7  * @authorWGS8  *9  */Ten  Public classBianliofbinarrytree { One       Public Static classTreeNode { A             intval = 0; -TreeNode left =NULL; -TreeNode right =NULL; the  -              PublicTreeNode (intval) { -                  This. val =Val; -  +             } -      } +     /** A * Method One: The simple method of the big talk data structure, can realize the operation.  at      * @paramargs -      */ -      -      Public voidPreorder (TreeNode node) { -         if(node==NULL)return; -System.out.print (node.val+ ""); in preorder (node.left); - preorder (node.right); to     } +      Public voidinorder (TreeNode node) { -         if(node==NULL)return; the inorder (node.left); *System.out.print (node.val+ ""); $ inorder (node.right);Panax Notoginseng     } -      Public voidPostorder (TreeNode node) { the         if(node==NULL)return; + Postorder (node.left); A Postorder (node.right); theSystem.out.print (node.val+ ""); +     } -     /** $ * Method Two: The method of the sword refers to the offer: Join the stack operation $      * @paramargs -      */ -      Public voidPreOrder2 (TreeNode node) { theStack<treenode> stack=NewStack<treenode>(); -         Wuyi          while(node!=NULL|| !Stack.empty ()) { the             //Walk the left subtree first -              while(node!=NULL){ WuSystem.out.print (node.val+ ""); - Stack.push (node); AboutNode=node.left;//left dial hand tree traversal ends, node==null, jump out of this loop $             }             -             //left dial hand tree traversal end, continue to traverse right sub-tree -             if(!Stack.isempty ()) { -Node=stack.pop ();//keep returning until the root node, node==null, i.e. stack==isempty () ANode=Node.right; +             }             the         } -     } $      Public voidInOrder2 (TreeNode node) { theStack<treenode> stack=NewStack<treenode>(); the          while(node!=NULL|| !Stack.isempty ()) { the              the              while(node!=NULL){             -Stack.push (node);//8 6 5 inNode=node.left;//6 5 Null the             } the              About             if(!Stack.isempty ()) { theNode=stack.pop ();//5 6 theSystem.out.print (node.val+ "");//5 6 theNode=node.right;//NULL +             } -         } the     }Bayi     //The post- traversal is not yet the     /*Public void PostOrder2 (TreeNode node) { the stack<treenode> stack=new stack<treenode> (); - While (Node!=null | | !stack.isempty ()) { - While (node!=null) { the Stack.push (node);//8 6 5 7 the NODE=NODE.LEFT;//6 5 NULL null the             } the if (!stack.isempty ()) { - Node=stack.pop ();//5 6 7 the node=node.right;//null 7 Null the              the             }94              the         } the     }*/ the     98      Public Static voidMain (string[] args) { AboutTreeNode root =NewTreeNode (8); -TreeNode Node1 =NewTreeNode (6);101TreeNode Node2 =NewTreeNode (10);102TreeNode Node3 =NewTreeNode (5);103TreeNode node4 =NewTreeNode (7);104TreeNode NODE5 =NewTreeNode (9); theTreeNode Node6 =NewTreeNode (11);106 107Root.left =Node1;108Root.right =Node2;109Node1.left =Node3; theNode1.right =Node4;111Node2.left =Node5; theNode2.right =Node6;113              theBianliofbinarrytree bianli=NewBianliofbinarrytree (); the Bianli.preorder (root); theSystem.out.println ("Pre-sequence traversal-------");117 Bianli.inorder (root);118System.out.println ("Middle order traversal-------");119 Bianli.postorder (root); -SYSTEM.OUT.PRINTLN ("The post-sequential traversal-------");121 Bianli.preorder2 (root);122System.out.println ("Method two:" Pre-sequence traversal-------");123 Bianli.inorder2 (root);124System.out.println ("Method two:" The middle sequence traversal-------"); the            //Bianli.postorder2 (root);126System.out.println ("Method two:" The post-sequential traversal-------");127     } - 129}

The Java implementation of the pre-order, middle order and post-sequence traversal of the tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.