Fastest recursive java code


  • 1
    N

    '''
    public class Solution {
    public TreeNode invertTree(TreeNode root) {
    if(root == null) {
    return root;
    }
    return invertedTree(root);
    }

    public TreeNode invertedTree(TreeNode root) {
        if(root == null) {
            return null;
        } else {
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
            invertedTree(root.left);
            invertedTree(root.right);
        }
        return root;
    }
    

    }

    '''


  • 0
    A

    @nikhashu Why did you create a new function? I suppose the code logic can be put in invertTree. Works the same.


  • 0
    D

    @aj2810 yes,you're right.I used to make the same mistake.


Log in to reply
 

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