Very Simple and concise java solution


  • 0
    J

    public List<Integer> preorderTraversal(TreeNode root) {
    Stack<TreeNode> stack = new Stack<>();
    stack.push(root);
    while(!stack.isEmpty()){
    TreeNode n = stack.pop();
    if(n!=null){
    list.add(n.val);
    stack.push(n.right);
    stack.push(n.left);
    }
    }
    return list;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.