Java 1ms iterative solution, no stack, no recursion.


  • 1
    S
        public void flatten(TreeNode root) {
            while (root != null) {
                if (root.left != null) {
                    TreeNode temp = root.left;
                    while (temp.right != null) temp = temp.right;
                    temp.right = root.right;
                    root.right = root.left;
                    root.left = null;
                }
                root = root.right;
            }
        }
    

Log in to reply
 

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