【leetcode】104-Maximum Depth of Binary Tree

problem

Maximum Depth of Binary Tree

 code

递归方法

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

 

 

 

 

参考

1.Maximum Depth of Binary Tree

posted on 2018-11-26 11:36  鹅要长大  阅读(128)  评论(0编辑  收藏  举报

导航