Java efficient post-order traversal solution


  • 0
    Y
    public void flatten(TreeNode root) {
        if(root == null) return;
        flatten(root.left);
        flatten(root.right);
        if(root.right != null || root.left != null) {
            TreeNode tmp = root;
            if(root.left != null) {
                tmp = root.left;
                while(tmp.right != null) {
                    tmp = tmp.right;
                }
                tmp.right = root.right;
                root.right = root.left;
                root.left = null;
            }
        }
    }

Log in to reply
 

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