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. SoapUI API + Groovy API + Difference with Java

    用soapUI进行webservice测试过程中,必不可少的要用到soapUI封装的代码.我们一起学习吧:) SoapUI 5.1.2 API:http://www.soapui.org/apidoc ...

  2. Python print格式化输出

    python中的print格式化输出,基本格式:"[字符串]%格式1[字符串]%格式2[字符串]....."%(string1,string2.....) 格式符号 ------- ...

  3. 数据库对象映射为java对象,不使用框架

    方法: public static <T> List<T> processResultSetToList(ResultSet rs, Class<T> clazz) ...

  4. SQL匹配顺序

    SELECT%DISTINCT%%FIELD%FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%COMMENT%

  5. mouseover 移入某个元素后停留一段时间再执行函授,我用于解决轮播图下面计数用的元素快速移入后会出BUG的问题。

    var stop; $(this).bind("mouseover",function(){ stop= setTimeout(function(){ },200); }).bin ...

  6. C#常用操作类库三(XML操作类)

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  7. sublime text3 快捷键设置

    //插入到key binding user 里面,浏览器安装路径修改成自己的路径 1[ //firefox测试快捷键 { "keys":["f3"], &quo ...

  8. C# 反射遍历对象

    在项目中需要遍历各种对象,可以通过如下方法遍历. /// <summary> /// 返回对象字符串 /// </summary> /// <param name=&qu ...

  9. HDU 2509 nim博弈

    Be the Winner Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. 论文笔记之: Deep Metric Learning via Lifted Structured Feature Embedding

    Deep Metric Learning via Lifted Structured Feature Embedding CVPR 2016 摘要:本文提出一种距离度量的方法,充分的发挥 traini ...