Why is this memory limit exceed


  • 0
    S
            if (root == null) return null ;
            Stack<TreeNode> s = new Stack<TreeNode>() ;
            s.push(root);
            while( s.peek().left != null || s.peek().right != null ){
                TreeNode temp = s.peek() ;
                if (temp.right != null) s.push(temp.right);
                if (temp.left != null) s.push(temp.left);
            }
            TreeNode head = s.pop();
            TreeNode prev = head ;
            while (!s.empty()){
                prev.left = s.pop();
                if(!s.empty()) prev.right = s.pop();
                prev = prev.right ;
            } 
            return head ;

Log in to reply
 

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