Why TLE??help me~


  • 0
    L

    public void flatten(TreeNode root) {

      if(root == null) return;
      TreeNode p = root;
      Deque<TreeNode> stack = new LinkedList<TreeNode>();
      TreeNode temp = new TreeNode(0);
      
      
      while(p != null || stack.peekFirst()!=null)
      {
    	  while(p != null)
    	  {
    		 stack.addFirst(p);
    		 temp.left = null;
    		 temp.right = p;
    		 temp = p;
    		 p = p.left;
    		 
    	  }
    	  
    	  if(stack.peekFirst() != null)
    		  p = stack.removeFirst().right;	  	  
      }
    
    }

Log in to reply
 

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