Help, why my solution was wrong?


  • 0
    G

    /**

    • Definition for a binary tree node.

    • public class TreeNode {

    • int val;
      
    • TreeNode left;
      
    • TreeNode right;
      
    • TreeNode(int x) { val = x; }
      
    • }
      */
      public class Solution {

      Queue<TreeNode> curQueue=new LinkedList<TreeNode>();
      Queue<TreeNode> nextQueue=new LinkedList<TreeNode>();
      List<Integer> list=new LinkedList<Integer>();
      public List<Integer> preorderTraversal(TreeNode root)
      {
      curQueue.add(root);

        while(!curQueue.isEmpty())
        {
      	  TreeNode curr=curQueue.poll();
      	  if(curr!=null)
      	  {
      	  list.add(curr.val);
      	  nextQueue.add(curr.left);
      	  nextQueue.add(curr.right);
      	  }
      	  
      	  if(curQueue.isEmpty())
      	  {
      		  curQueue=nextQueue;
      	  }
      	  
        }
       
        return list;
      

      }
      }

    I cannot pass test [1,4,3,2]. My result was [1,4,3,2], Its expectation is [1,4,2,3]... Please help me with this...


Log in to reply
 

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