Traversal of the Java tree (recursive vs. non-recursive)

Source: Internet
Author: User


Package Wangchaopa practice work. Com.leetcode;
Import java.util.ArrayList;
Import Java.util.Stack;
Class TreeNode
{
TreeNode left;
TreeNode right;
int Val;

TreeNode (int x)
{
val = x;
}
}
public class Treetrivel
{
Test
public static void Main (string[] args)
{
Treetrivel AA = new Treetrivel ();
TreeNode treeNode1 = new TreeNode (1);
TreeNode treeNode2 = new TreeNode (2);
TreeNode treeNode3 = new TreeNode (3);
TreeNode treeNode4 = new TreeNode (4);
TreeNode TREENODE5 = new TreeNode (5);
TreeNode treeNode6 = new TreeNode (6);
TreeNode TreeNode7 = new TreeNode (7);
Treenode1.left = TreeNode2;
Treenode1.right = TreeNode3;
Treenode2.left = TreeNode4;
Treenode2.right = TREENODE5;
Treenode3.left = TreeNode6;
Treenode3.right = TreeNode7;

Three methods have a little effect on each other, so run alone
/*
* Arraylist<integer> postorder = aa.postordertraversal (treeNode1); For
* (Integer Temp:postorder) {System.out.print (temp + "");}
* SYSTEM.OUT.PRINTLN ();
*/
/*
* Arraylist<integer> preorder = aa.preordertraversal (TREENODE1); For
* (Integer Temp:preorder) {System.out.print (temp + "");}
* SYSTEM.OUT.PRINTLN ();
*/

arraylist<integer> inorder = aa.inordertraversal (treeNode1);
for (Integer Temp:inorder)
{
System.out.print (temp + "");
}
System.out.println ();
}

Non-recursive mid-order traversal
/*
* The most important thing is to determine if the node P has no node, if there is a p.left into the stack, and make p.left=null, otherwise will p.val saved to the list, and Judge P.
* Right is NULL if not NULL then put p.right into stack
*/
Public arraylist<integer> inordertraversal (TreeNode root)
{
arraylist<integer> res = new arraylist<integer> ();
if (root = = null)
{
return res;
}
stack<treenode> stack = new stack<treenode> ();
Stack.push (root);
while (!stack.empty ())
{
TreeNode temp = Stack.peek ();
if (Temp.left = = null)
{
TreeNode p = stack.pop ();
Res.add (P.val);
if (p.right! = null)
{
Stack.push (Temp.right);
}

}
Else
{
Stack.push (Temp.left);
Temp.left = null;
}
}
return res;
}

/*
* Non-recursive post-order traversal idea: To ensure that the root node in the left child and right child access before access, so for any node p, first put it into the stack.
* If p does not exist left child and right child, then direct access; otherwise the right child of P and the left child will be placed in the stack and then the P's left child node is assigned null, so that every time you take the top element of the stack
* Left child is visited in front of right child, left child and right child are visited in front of root node.
*/
Public arraylist<integer> postordertraversal (TreeNode root)
{
arraylist<integer> res = new arraylist<integer> ();
if (root = = null)
{
return res;
}
stack<treenode> stack = new stack<treenode> ();
Stack.push (root);
while (!stack.isempty ())
{
TreeNode temp = Stack.peek ();
if (Temp.left = = NULL && Temp.right = = null)
{
TreeNode pop = Stack.pop ();
Res.add (Pop.val);
}
Else
{
if (temp.right! = null)
{
Stack.push (Temp.right);
Temp.right = null;
}
if (temp.left! = null)
{
Stack.push (Temp.left);
Temp.left = null;
}
}
}
return res;
}

Non-recursive pre-sequence traversal
/*
* P.val is saved directly into the list and then determines if p.right is null if it is not NULL then p.right into the stack and then judging
* P.left is NULL if not NULL then p.left into the stack
*/
Public arraylist<integer> preordertraversal (TreeNode root)
{
arraylist<integer> res = new arraylist<integer> ();
if (root = = null)
{
return res;
}
stack<treenode> stack = new stack<treenode> ();
Stack.push (root);
while (!stack.empty ())
{
TreeNode temp = Stack.pop ();
Res.add (Temp.val);

if (temp.right! = null)
{
Stack.push (Temp.right);
}
if (temp.left! = null)
{
Stack.push (Temp.left);
}
}
return res;
}
}

Traversal of the Java tree (recursive vs. non-recursive)

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.