您的位置:首页 > 其它

LeetCode 104. Maximum Depth of Binary Tree

2016-03-22 15:38 405 查看
废话不多说,先看题目:

Given a binary tree, find its maximum depth.

题目意思:就是要求得到二叉树的深度或者高度

学过数据结构对这个肯定不会陌生,典型的递归:

/**
* 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;
else
{
int l=maxDepth(root->left);
int r=maxDepth(root->right);
return r>l?r+1:l+1;
}

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: