3ms simple C recursive, accepted


  • 0
    V
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    int maxDepth(struct TreeNode* root) {
        int maxL = 0;
        int maxR = 0;
        if (root == NULL) return 0;
        else if (root->left == NULL && root->right == NULL) return 1;
        if (root->left != NULL) {
            maxL = maxDepth(root->left);
        }
        if (root->right != NULL) {
            maxR = maxDepth(root->right);
        }
        if (maxR > maxL) return maxR + 1;
        else return maxL + 1;   
    }

Log in to reply
 

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