leetcode 104. Maximum Depth of Binary Tree(求一棵树的深度)

题目链接

题意:求一棵树的深度。。。。

思路:。。。定义搞即可。。按照左右子树中大的算。。。因为据说是经典题(虽然并不觉得2333。。。所以记录下。。。

 1/**
 2 * Definition for a binary tree node.
 3 * struct TreeNode {
 4 *     int val;
 5 *     TreeNode *left;
 6 *     TreeNode *right;
 7 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8 * };
 9 */
10class Solution {
11public:
1        int dfs(TreeNode* root)
2        {
3            if (root==NULL) return 0;
4            return max(dfs(root->left),dfs(root->right))+1;
5        }
1    int maxDepth(TreeNode* root){
2        if (root==NULL) return 0;
3        int res = dfs(root);
4        return res;
    }
};