Leetcode 104. Maximum Depth of Binary Tree

 

 

 

 

 

求树的最大深度,如果树为空,则返回0
否则返回左最大深度和右最大深度的最大值,进行加一

/**
 * 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 == NULL) return 0;
        return max(maxDepth(root->left),maxDepth(root->right)) + 1;
    }
};

 

posted @ 2020-02-07 02:43  SteveYu  阅读(115)  评论(0编辑  收藏  举报