C++ 2 lines recursive solution


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

  • 0
    R

    You can make it one-line-solution with ternary condition operator!


Log in to reply
 

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