Easy-understanding Java solution based on Morris Traversal


  • 0
    Y
    public void flatten(TreeNode root) {
        while(root != null){
            if(root.left != null && root.right != null){
                TreeNode tmp = root.left;
                while(tmp.right != null) tmp = tmp.right;
                tmp.right = root.right;
            }
            if(root.left != null)
                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.