Skip to content

Instantly share code, notes, and snippets.

@goyuninfo
Last active December 31, 2015 16:59
Show Gist options
  • Save goyuninfo/8017269 to your computer and use it in GitHub Desktop.
Save goyuninfo/8017269 to your computer and use it in GitHub Desktop.
public class preOrderTreeNodeStack {
public ArrayList<Object> preorderTraversal(TreeNode root) {
ArrayList<Object> returnList = new ArrayList<Object>();
if(root == null)
return returnList;
Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode n = stack.pop();
returnList.add(n.getNodeValue());
if(n.rightNode() != null){
stack.push(n.rightNode());
}
if(n.leftNode() != null){
stack.push(n.leftNode());
}
}
return returnList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment