码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树的最大深度

时间:2017-07-22 18:15:31      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:c++   png   treenode   二叉树   int   .com   最大   code   ==   

代码(C++):

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: An integer
*/
int maxDepth(TreeNode *root) {
// write your code here
if (root == NULL) {
return 0;
}
int left = maxDepth(root -> left);
int right = maxDepth(root -> right);
return (left > right) ? left + 1 : right + 1;
}
};

技术分享

二叉树的最大深度

标签:c++   png   treenode   二叉树   int   .com   最大   code   ==   

原文地址:http://www.cnblogs.com/majixian/p/7221823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!