Minimum Depth of Binary Tree

OJ: https://oj.leetcode.com/problems/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 .

/**
* 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) {
if(root == NULL) return 0;
if(root->left == NULL && root->right == NULL) return 1;
int l = minDepth(root->left);
int r = minDepth(root->right);
return (l && r) ? min(l, r) + 1 : (l+r+1);
}
};

Balanced Binary Tree

OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

思想: 先序遍历。既要返回左右子树判断的结果,又要返回左右子树的深度进行再判断。

所以要么返回一个 pair<bool, int>, 要么函数参数增加一个引用来传递返回值。

方法1:返回一个 pair<bool, int>: (更简洁)

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
typedef pair<bool, int> Pair;
Pair judge(TreeNode *root) {
if(root == NULL) return Pair(true, 0);
Pair L = judge(root->left);
Pair R = judge(root->right);
return Pair(L.first && R.first && abs(L.second-R.second) < 2, max(L.second, R.second)+1);
}
class Solution {
public:
bool isBalanced(TreeNode *root) {
return judge(root).first;
}
};

方法二: 增加一个引用

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
bool judge(TreeNode *root, int& depth) {
if(root == NULL) { depth = 0; return true; }
int l, r;
if(judge(root->left, l) && judge(root->right, r)) {
depth = 1 + max(l, r);
if(l-r <= 1 && l-r >= -1) return true;
else return false;
}
}
class Solution {
public:
bool isBalanced(TreeNode *root) {
int depth;
return judge(root, depth);
}
};

Maximum Depth of Binary Tree

OJ: https://oj.leetcode.com/problems/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.

注: 此题略水,于此带过。

class Solution {
public:
int maxDepth(TreeNode *root) {
return root ? max(maxDepth(root->left), maxDepth(root->right))+1 : 0;
}
};

33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. Maximum Depth of Binary Tree 解答

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

  8. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  9. (二叉树 BFS DFS) 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 ...

随机推荐

  1. 我的qq邮箱的GPG公钥

    呵呵,赶一波潮流,我的邮箱 175420840@qq.com 的GPG公钥如下,也可以在这里直接下载.具体可参见阮一峰的<GPG入门教程>. -----BEGIN PGP PUBLIC K ...

  2. WP8.1 Study17:网络之后台下载/上传及HttpClient

    一.后台下载/上传 1.简介 使用BackgroundTransferGroup可以十分方便操作上传及下载文件,BackgroundDownloader和BackgroundUploader类中的方法 ...

  3. 当BPM业务流程管理遇上制造业

    2015年5月,K2正式与赛科利签约,准备上线核心采购类流程,包括PR.PO.Payment.供应商管理等. 上海赛科利汽车模具技术应用有限公司隶属于上汽集团,现有员工2300余人.为解决汽车“安全” ...

  4. Java mysql 日期相关

    获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowString { ...

  5. Adaboost 2

    本文不定期更新.原创文章,转载请注明出处,谢谢. Adaboost是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器(弱分类器),然后把这些弱分类器集合起来,构成一个更强的最终分类器(强分类 ...

  6. VS2015打开工程 未能正确加载“”包的问题

    启动vs2015专业版时,出现类似于这样的提示框,有好几个,点击是或否,但下次打开还是会出现.寻找了网上的一些解决办法,例如用vs命令窗口或其他,但都无疾而终,下面提供的这个办法,顺利解决此问题 1. ...

  7. Android studio的深坑 导jar包重复的异常处理

    导包重复这个问题折磨了整整一天!! 第一个方法在gradle文件下看看有没有重复的包  如果有那么一切都是浮云分分钟搞定 ,最可恶的是在gradle里面没有. 在gradle里面找不到的话就要考虑一下 ...

  8. Simple Maven Project

    为pom.xml添加组织,法律和开发人员信息 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...

  9. Android 学习第4课,一些小知识

    java语言的注释有如下3种: 1. // 2./* 注释内容 */ 3./**     注释内容    */       这种叫文档注释,这种注释常被javaDoc文档工具读取作为 JavaDoc文 ...

  10. WAMPSERVER 与 IIS冲突 及如何修改网站根目录

    如果之前打开过IIS服务或者用过xampp则输入localhost时,浏览器显示如下: 这说明WAMPSERVER 与 IIS服务的默认端口冲突: 可以测试80端口是否被占用: 如上端口被IIS服务占 ...