Error when running test example {1,#,2,3} with java


  • 1
    V

    public class Solution {
    public void flatten(TreeNode root) {
    TreeNode last = helper(root);
    }

    private TreeNode helper(TreeNode root) {
        TreeNode last;
        TreeNode temp;
        if (root == null) {
            return null;
        }
        if (root.left == null && root.left == null) {
            return root;
        }
        if (root.right == null && root.left != null) {
            last = helper(root.left);
            root.right = root.left;
            root.left = null;
            
            return last;
        }
        if (root.right != null && root.left == null) {
            last = helper(root.right);
            return last;
        }
        if (root.right != null && root.left != null) {
            temp = root.right;
            root.right = root.left;
            root.left = null;
            last = helper(root.right);
            last.right = temp;
            return helper(temp);
        }
        return null;
    }
    

    }

    such code raises error when running:
    Input: {1,#,2,3}
    Output: {1,#,2,3}
    Expected: {1,#,2,#,3}

    cannot figure out the reason. Thanks


Log in to reply
 

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