8ms C++ maximum depth of binary tree


  • 0
    S
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
     /*1*/
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if (root== NULL) return 0;
            int xl=1,xr=1;
            if (root->left == NULL) xl=1; else xl=maxDepth(root->left)+1;
            if (root->right == NULL) xr=1; else xr=maxDepth(root->right)+1;
                return max(xl,xr);
                
        }
    };
    /*2
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if (root== NULL) return 0;
            return max(maxDepth(root->left),maxDepth(root->right))+1;
                
        }
    };
    */

Log in to reply
 

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