Preorder Traversal Iterative
Page 1 of 1
Preorder Traversal Iterative
public static preorder(TreeNode root){
Stack stack=new Stack();
stack.push(root);
while(true){
TreeNode temp=stack.pop();
if(temp.left){
stack.push(temp);
stack.push(temp.left);
}
else{
TreeNode right=temp.right;
if(right!=null)
stack.push(right);
}
}
}
Stack stack=new Stack();
stack.push(root);
while(true){
TreeNode temp=stack.pop();
if(temp.left){
stack.push(temp);
stack.push(temp.left);
}
else{
TreeNode right=temp.right;
if(right!=null)
stack.push(right);
}
}
}
skyboard- Posts : 31
Join date : 2011-09-03
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|