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

Posted by 111qqz on Wednesday, February 22, 2017

TOC

题目链接

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

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

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