How to get[ ],I don't know what's wrong!


  • 0
    C

    public class Solution {
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
    java.util.Stack<TreeNode> stack=new java.util.Stack<TreeNode>();
    if(root==null)
    return new ArrayList<Integer>();
    java.util.ArrayList<Integer> list=new java.util.ArrayList<Integer>();
    TreeNode head=root;
    while(stack.size()!=0||head!=null){
    while(head!=null){
    System.out.println(head.val);
    list.add(head.val);
    stack.push(head);
    head=head.left;
    }
    head=stack.pop();
    if(head.right!=null){
    head=head.right;
    }else{
    if(stack.size()>0){
    head=stack.pop();
    head=head.right;
    }else{
    break;
    }

    		}
    	}
    	return list;
    }
    

    }


Log in to reply
 

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