Leetcode Maximum Depth of Binary Tree
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.
递归求解
int maxDepth(TreeNode *root){
return root? +max(maxDepth(root->left), maxDepth(root->right)) : ;
}
非递归求解
struct Node{
TreeNode *node;
int depth;
Node(TreeNode *a = NULL , int d = ):node(a), depth(d);
}; int maxDepth1(TreeNode *root){
if(root == NULL) return ;
queue<Node> que;
Node rootNode(root,);
que.push(rootNode);
int res = ;
while(!que.empty()){
Node p = que.front();que.pop();
res = p.depth;
if(p.node->left) que.push(Node(p.node->left,p.depth+));
if(p.node->right ) que.push(Node(p.node->right,p.depth+));
}
return res;
}
Leetcode Maximum Depth of Binary Tree的更多相关文章
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [Leetcode] Maximum depth of binary tree二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
- leetcode Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode:Maximum Depth of Binary Tree【Python版】
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LeetCode:Maximum Depth of Binary Tree_104
LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...
随机推荐
- js DOM对象
查找元素 根据id找 document.getElementById("b"): 根据class找 document.getElementsByClassName("aa ...
- Android -- FragmentActivity添加Fragment的序列图
FragmentActivity添加Fragment的序列图
- TCP 四次握手
TCP协议中的三次握手和四次挥手(图解) http://blog.csdn.net/whuslei/article/details/6667471/
- OCJP(1Z0-851) 模拟题分析(九)over
Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...
- php开发(TP框架使用)
由于最近玩了PHP,我向来有个原则,学一门服务端语言至少得玩两个框架,前段时间用PHP写了些demo+小项目,看见身边有人在使用TP,于是乎鼓捣学习学习.如何学,无非也就是做个小demo:就目前看来现 ...
- js this的使用举例
js this的使用举例 <script type="text/javascript"> function test(obj){ obj.style.width= ob ...
- 解决linux下unzip中文有乱码的问题
xxx.zip 中有中文的文件,在linux下unzip就会有乱码. 解决办法:安装7zip 去http://sourceforge.net/projects/p7zip/files/latest/d ...
- Chrome书签被篡改之后的恢复
chrome书签和备份存放的路径:(XXXX为用户名)(AppData文件夹为隐藏文件夹) \Users\XXXX\AppData\Local\Google\Chrome\User Data\Defa ...
- Ubuntu 安装OpenCV3.0.0
Ubuntu安装OpenCV3.0.0 为了看看opencv3.0的HDR效果,尝试安装opencv3.0到ubuntu12.04上面,安装了好几次终于成功了. 参考博客: http://www.sa ...
- barabasilab-networkScience学习笔记5- Barabási-Albert 模型
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...