LeetCode:Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
算法1:dfs递归的求解
class Solution {
public:
int minDepth(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(root == NULL)return ;
int res = INT_MAX;
dfs(root, , res);
return res;
}
void dfs(TreeNode *root, int depth, int &res)
{
if(root->left == NULL && root->right == NULL && res > depth)
{res = depth; return;}
if(root->left)
dfs(root->left, depth+, res);
if(root->right)
dfs(root->right, depth+, res);
}
};
还有一种更直观的递归解法,分别求左右子树的最小深度,然后返回左右子树的最小深度中较小者+1
class Solution {
public:
int minDepth(TreeNode *root) {
if(root == NULL)return ;
int minleft = minDepth(root->left);
int minright = minDepth(root->right);
if(minleft == )
return minright + ;
else if(minright == )
return minleft + ;
else return min(minleft, minright) + ;
}
};
算法2:层序遍历二叉树,找到最先遍历到的叶子的层数就是树的最小高度
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//层序遍历,碰到第一个叶子节点就停止,NULL作为每一层节点的分割标志
if(root == NULL)return ;
int res = ;
queue<TreeNode*> Q;
Q.push(root);
Q.push(NULL);
while(Q.empty() == false)
{
TreeNode *p = Q.front();
Q.pop();
if(p != NULL)
{
if(p->left)Q.push(p->left);
if(p->right)Q.push(p->right);
if(p->left == NULL && p->right == NULL)
{
res++;
break;
}
}
else
{
res++;
if(Q.empty() == false)Q.push(NULL);
}
}
return res;
}
};
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.
算法1:dfs递归求解
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(root == NULL)return ;
int res = INT_MIN;
dfs(root, , res);
return res;
}
void dfs(TreeNode *root, int depth, int &res)
{
if(root->left == NULL && root->right == NULL && res < depth)
{res = depth; return;}
if(root->left)
dfs(root->left, depth+, res);
if(root->right)
dfs(root->right, depth+, res);
}
};
同上一题
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root == NULL)return ;
int maxleft = maxDepth(root->left);
int maxright = maxDepth(root->right);
if(maxleft == )
return maxright + ;
else if(maxright == )
return maxleft + ;
else return max(maxleft, maxright) + ;
}
};
算法2:层序遍历,树的总层数就是树的最大高度
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//层序遍历计算树的层数即可,NULL作为每一层节点的分割标志
if(root == NULL)return ;
int res = ;
queue<TreeNode*> Q;
Q.push(root);
Q.push(NULL);
while(Q.empty() == false)
{
TreeNode *p = Q.front();
Q.pop();
if(p != NULL)
{
if(p->left)Q.push(p->left);
if(p->right)Q.push(p->right);
}
else
{
res++;
if(Q.empty() == false)Q.push(NULL);
}
}
return res;
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440059.html
LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree的更多相关文章
-
[Leetcode][JAVA] Minimum Depth of Binary Tree &;&; Balanced Binary Tree &;&; Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
-
LEETCODE —— binary tree [Same Tree] &;&; [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
-
【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree
解法一:递归 int maxDepth(TreeNode* root) { if (root == NULL) ; int max_left_Depth = maxDepth(root->lef ...
-
[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] 104. Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
-
2016.6.26——Maximum Depth of Binary Tree
Maximum Depth of Binary Tree 本题收获 1.树时使用递归 2.注意边界条件时输出的值,仔细阅读题意,若是面试时,问清边界条件. 题目: Given a binary tre ...
-
Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
-
[LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree
The problem 1: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes ...
-
[LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
-
将博客搬至CSDN
将博客搬至CSDN将博客搬至CSDN将博客搬至CSDN将博客搬至CSDN
-
回传数据startActivityForResult()
1.调用者Activity01开启新的界面选用startActivityForResult(intent,requestCode);在Activity01中Intent intent=new Inte ...
-
[C#]exchange发送,收件箱操作类
最近项目中需要用到exchange的操作,就参照msdn弄了一个简单的操作类.目前先实现了,发送邮件和拉取收件箱的功能,其他的以后在慢慢的添加. using Microsoft.Exchange.We ...
-
linux下利用curl监控网页shell脚本
#!/bin/bash smail() {mail -s "$1" gjw_apparitor@gmail.com <<EOF$1$2====report time: ...
-
Ubuntu 关闭锁屏界面的 on-screen keyboard
试了试屏幕键盘,在 系统设置里开启了,又关了,但是在屏幕解锁时总是出现 screen keyboard,老烦人了,不知到在哪里关闭了,系统设置里面都关了,网上搜了解决办法,原来在这里 把 show w ...
-
过生日,也要学学哈,这次是SHELL的GETOPTS
今天是WEBSOCKET,, 先完成一个SHELL的GETOPS,周一就用得着. #!/bin/bash echo "usage: ./$0 -t (prism|opscripts)&quo ...
-
美工代码注意事项(html+div+css+js)
window.location.href的target控制 在使用框架时,经常会对框架子页面进行页面引导的情况,如果只是简单的设置location. href="",会使得整个页面 ...
-
js 中文排序
/** * 比较函数 * @param {Object} param1 要比较的参数1 * @param {Object} param2 要比较的参数2 * @return {Number} 如果pa ...
-
document 例子
<!DOCTYPE html> <html> <head> <title></title> <script type="te ...
-
ubuntu中安装myeclipse提示Insufficient Memory解决方法
经过查看资料发现出现这个问题的原因是因为计算机中swap分区的内存不足,或者没有创建swap分区,google中http://www.bkjia.com/webzh/1003601.html提供了一种 ...