public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ret = new LinkedList<>();
if(root == null)
return ret;
Stack<Command> stack = new Stack<>();
stack.push(Command("go",root));
while(!stack.isEmpty()){
Command temp = stack.pop();
if(temp.res == "to"){
ret.add(temp.node.val);
}else{
if(temp.node.right != null)
stack.push(Command("go",temp.node.right));
if(temp.node.left != null)
stack.push(Command("go",temp.node.left));
stack.push(Command("to",temp.node));
}
}
return ret;
}
}