Java solution refer to Mirris traversal using constant place


  • 8
    L
    public void flatten(TreeNode root) {
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null){
            if(cur.left == null) cur = cur.right;
            else {
                prev = cur.left;
                while(prev.right != null) prev = prev.right;
                prev.right = cur.right;
                cur.right = cur.left;
                cur.left = null;
            }
        }
    }

  • 0
    C

    brilliant and easy to understand solution!


Log in to reply
 

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