leetcode 104 Maximum Depth of Binary Tree
2016-06-05 09:35
281 查看
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Subscribe to see which companies asked this question
/**
* 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 preOrder(TreeNode *root, int hight, int &maxH) {
if(root!=NULL) {
if(hight>maxH) maxH=hight;
preOrder(root->left, hight+1, maxH);
preOrder(root->right, hight+1, maxH);
}
return maxH;
}
int maxDepth(TreeNode* root) {
int maxH = 0;
if(root==NULL) return 0;
return preOrder(root, 1, maxH);
}
};
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Subscribe to see which companies asked this question
/**
* 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 preOrder(TreeNode *root, int hight, int &maxH) {
if(root!=NULL) {
if(hight>maxH) maxH=hight;
preOrder(root->left, hight+1, maxH);
preOrder(root->right, hight+1, maxH);
}
return maxH;
}
int maxDepth(TreeNode* root) {
int maxH = 0;
if(root==NULL) return 0;
return preOrder(root, 1, maxH);
}
};
相关文章推荐
- NHOI2016的解题报告
- iOS开发中常用第三方库的整理
- 【搜索那些事】细谈lucene(三)lucene核心API简介
- 正则表达式--非捕获
- CodeForces 598A Tricky Sum 数列求和 暑期小练习Q
- 【搜索那些事】细谈lucene(二)lucene搜索程序组件详解
- Squid zph-qos 选项
- mysql windows service config though mysql info for next new OS or new PC
- 【搜索那些事】细谈lucene(一)初识全文资源检索框架lucene
- HTML5学习笔记-基础篇2-新元素概览
- 1183 泥泞的道路
- ubuntu系统如何屏幕截图
- ThinkPHP函数详解:U方法
- iOS面试问题汇总
- 2016年团体程序设计天梯赛-初赛 - 后天 - Java
- [转]机器学习和计算机视觉----数学基础
- Linux学习之字符处理命令
- 2016年团体程序设计天梯赛-初赛 - 奇偶分家 - Java
- 29款 业务规则引擎开源软件
- 伟哥大数据入门教程一