[Leetcode] [JAVA] Binary tree preorder traversal, binary tree inorder traversal, binary tree postorder traversal

Source: Internet
Author: User

Binary Tree Preorder Traversal:

Given a binary tree, return the preorder traversal of its nodes ' values.

For example:
Given binary Tree {1,#,2,3} ,

   1         2    /   3

Return [1,2,3] .

Note:recursive solution is trivial, could do it iteratively?

Recursive pre-order traversal is not used and can be implemented with the help of stacks. For a particular node, the sequence of pre-order traversal is: root, left, and right. So into the stack order for right, left, root. Because the root node traversal and expansion (research its right and left node) is the same, so the root node out of the stack can join the traversal results, and then study its right and left node, if not empty can be stacked.

1   PublicList<integer>preordertraversal (TreeNode root) {2list<integer> ls =NewArraylist<integer>();3         if(root==NULL)4             returnls;5Stack<treenode> st =NewStack<treenode>();6 St.push (root);7         8          while(!st.isempty ())9         {TenTreeNode temp =St.pop (); One Ls.add (temp.val); A             if(temp.right!=NULL) - St.push (temp.right); -             if(temp.left!=NULL) the St.push (temp.left); -         } -         returnls; -}

Binary Tree inorder Traversal

Given a binary tree, return the inorder traversal of its nodes ' values.

For example:
Given binary Tree {1,#,2,3} ,

   1         2    /   3

Return [1,3,2] .

Note:recursive solution is trivial, could do it iteratively?

The middle sequence traversal is more complex than the pre-order traversal, which is mainly the need to distinguish whether the nodes need to be expanded or traversed directly. Generally speaking, the first access node is expanded, and it is re-entered into the stack, and the second time it is accessed from the stack counts into the traversal. HashSet is used here to determine if it has been visited.

The stacking order is right, root, and left (because the sequence traversal order is Zogen right)

1  PublicList<integer>inordertraversal (TreeNode root) {2list<integer> ls =NewArraylist<integer>();3         if(root==NULL)4             returnls;5Stack<treenode> st =NewStack<treenode>();6hashset<treenode> HS =NewHashset<treenode>();7         8 St.push (root);9          while(!st.isempty ())Ten         { OneTreeNode temp =St.pop (); A             if(Hs.contains (temp)) -             { - Ls.add (temp.val); the                 Continue; -             } - Hs.add (temp); -             if(temp.right!=NULL) + St.push (temp.right); - St.push (temp); +             if(temp.left!=NULL) A St.push (temp.left); at         } -         returnls; -}

Binary Tree postorder Traversal

Given a binary tree, return the postorder traversal of its nodes ' values.

For example:
Given binary Tree {1,#,2,3} ,

   1         2    /   3

Return [3,2,1] .

Note:recursive solution is trivial, could do it iteratively?

Same as the middle sequence traversal, except that the stacking order is root, right, left (order of sequential traversal is left, right, root)

1  PublicList<integer>postordertraversal (TreeNode root) {2list<integer> ls =NewArraylist<integer>();3         if(root==NULL)4             returnls;5Stack<treenode> st =NewStack<treenode>();6hashset<treenode> HS =NewHashset<treenode>();7         8 St.push (root);9          while(!st.isempty ())Ten         { OneTreeNode temp =St.pop (); A             if(Hs.contains (temp)) -             { - Ls.add (temp.val); the                 Continue; -             } - Hs.add (temp); - St.push (temp); +             if(temp.right!=NULL) - St.push (temp.right); +             if(temp.left!=NULL) A St.push (temp.left); at         } -         returnls; -}

[Leetcode][java] binary tree preorder traversal, binary tree inorder traversal, binary tree postorder traversal

Related Article

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.