您的位置:首页 > 其它

LeetCode 104. Maximum Depth of Binary Tree

2016-10-21 09:07 357 查看

描述

求树的深度

解决

递归。

/**
* 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)
return 0;
return 1 + max(maxDepth(root -> right), maxDepth(root -> left));
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode