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 ...
随机推荐
- EmguCV学习 与opencv的区别和联系
openCV是因特尔的一个开源的视觉库,里面几乎包含了所有的图像处理的经典算法,并且采用C和少量的C++编写,运行效率很高,对于做图像处理这方面工作的,认识opencv是必须的工作.不过opencv有 ...
- DOM – 7.动态创建DOM + 8.innerText innerHTML value
7.动态创建DOM 8.innerText innerHTML value 7+8 练习:案例:点击按钮动态增加网站列表,分两列,第一列为网站的名字,第二列为带网站超链接的网站名.增加三行常见网站 ...
- log4j设置日志格式为UTF-8
想要log4j输出的日志文件的编码格式为UTF-8.正常情况下只需要添加下述的代码即可: log4j.appender.appender_name.Encoding=UTF-8 但是在spring与l ...
- ORA-1461 encountered when generating server alert SMG-3500
Doc ID 461911.1 Patch 6602742 Applies to: Oracle Database - Enterprise Edition - Version 10.2.0.3 an ...
- C#从Image上读取文本
今天通过C#来实现一个读取Image上文本的功能. 1. 环境准备: 1). 下载 Microsoft Office SharePoint Designer 2007. 2). 安装请参考KB:htt ...
- hdu 2891 中国剩余定理
从6点看到10点,硬是没算出来,早知道玩游戏去了,艹,明天继续看 不爽,起来再看,终于算是弄懂了,以后超过一个小时的题不会再看了,不是题目看不懂,是水平不够 #include<cstdio> ...
- Windows硬件断点-实现单步异常
触犯单步异常 改变的是当前Eflags 而不是触发异常的Eflags 也就是 PUSHF MOV EAX, DWORD PTR[ESP] OR EAX, 0x100 MOV D ...
- 【转】【异常处理】Incorrect string value: '\xF0\x90\x8D\x83...' for column... Emoji表情字符过滤的Java实现
http://blog.csdn.net/shootyou/article/details/44852639 Emoji表情字符现在在APP已经广泛支持了.但是MySQL的UTF8编码对Emoji字符 ...
- LR检查点的if (status == LR_FAIL)写法
C语言例子:登录失败返回一个信息 int status = web_url("Login", "URL=https://secure.computing.com//log ...
- java的重载、覆盖和隐藏的区别
重载:方法名相同,但参数不同的多个同名函数 注意:1.参数不同的意思是参数类型.参数个数.参数顺序至少有一个不同 2.返回值和异常以及访问修饰符,不能作为重载的条件(因为对于匿名调用,会出现歧义,eg ...