LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

时间:2022-08-31 18:43:33

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的更多相关文章

  1. &lbrack;Leetcode&rsqb;&lbrack;JAVA&rsqb; Minimum Depth of Binary Tree &amp&semi;&amp&semi; Balanced Binary Tree &amp&semi;&amp&semi; Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  2. LEETCODE —— binary tree &lbrack;Same Tree&rsqb; &amp&semi;&amp&semi; &lbrack;Maximum Depth of Binary Tree&rsqb;

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  3. 【LeetCode 104&lowbar;二叉树&lowbar;遍历】Maximum Depth of Binary Tree

    解法一:递归 int maxDepth(TreeNode* root) { if (root == NULL) ; int max_left_Depth = maxDepth(root->lef ...

  4. &lbrack;LeetCode&rsqb; Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. &lbrack;LeetCode&rsqb; 104&period; Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. 2016&period;6&period;26——Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree 本题收获 1.树时使用递归 2.注意边界条件时输出的值,仔细阅读题意,若是面试时,问清边界条件. 题目: Given a binary tre ...

  7. Leetcode &vert; Minimum&sol;Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  8. &lbrack;LeetCode&num;104&comma; 111&rsqb;Maximum Depth of Binary Tree&comma; Minimum Depth of Binary Tree

    The problem 1: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes ...

  9. &lbrack;LeetCode&rsqb; Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. 将博客搬至CSDN

    将博客搬至CSDN将博客搬至CSDN将博客搬至CSDN将博客搬至CSDN

  2. 回传数据startActivityForResult&lpar;&rpar;

    1.调用者Activity01开启新的界面选用startActivityForResult(intent,requestCode);在Activity01中Intent intent=new Inte ...

  3. &lbrack;C&num;&rsqb;exchange发送,收件箱操作类

    最近项目中需要用到exchange的操作,就参照msdn弄了一个简单的操作类.目前先实现了,发送邮件和拉取收件箱的功能,其他的以后在慢慢的添加. using Microsoft.Exchange.We ...

  4. linux下利用curl监控网页shell脚本

    #!/bin/bash smail() {mail -s "$1" gjw_apparitor@gmail.com <<EOF$1$2====report time: ...

  5. Ubuntu 关闭锁屏界面的 on-screen keyboard

    试了试屏幕键盘,在 系统设置里开启了,又关了,但是在屏幕解锁时总是出现 screen keyboard,老烦人了,不知到在哪里关闭了,系统设置里面都关了,网上搜了解决办法,原来在这里 把 show w ...

  6. 过生日,也要学学哈,这次是SHELL的GETOPTS

    今天是WEBSOCKET,, 先完成一个SHELL的GETOPS,周一就用得着. #!/bin/bash echo "usage: ./$0 -t (prism|opscripts)&quo ...

  7. 美工代码注意事项(html&plus;div&plus;css&plus;js)

    window.location.href的target控制 在使用框架时,经常会对框架子页面进行页面引导的情况,如果只是简单的设置location. href="",会使得整个页面 ...

  8. js 中文排序

    /** * 比较函数 * @param {Object} param1 要比较的参数1 * @param {Object} param2 要比较的参数2 * @return {Number} 如果pa ...

  9. document 例子

    <!DOCTYPE html> <html> <head> <title></title> <script type="te ...

  10. ubuntu中安装myeclipse提示Insufficient Memory解决方法

    经过查看资料发现出现这个问题的原因是因为计算机中swap分区的内存不足,或者没有创建swap分区,google中http://www.bkjia.com/webzh/1003601.html提供了一种 ...