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        }
 6
 7    int maxDepth(TreeNode* root){
 8        if (root==NULL) return 0;
 9        int res = dfs(root);
10        return res;
11
12
13    }
14};