simple intuitive java solution


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

Log in to reply
 

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