The problem 1:

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.

My analysis:

The recursion solution for this problem is very elegant!
It include some skills we usually use in implenting recursive program for tree.
1. the base case for tree traversal is the leaf node's empty child(null), not the leaf node itself.

if (cur_root == null)
return 0;

2. when we calculate the path from current node to the leaf node, we calculate it from bottom to surface. At each node, we plus one, and return it to previous level recursion.
3. We use Math.math() function to get the longest path for either sub-tree.

return Math.max(helper(cur_root.left), helper(cur_root.right)) + 1; 

My solution:

public class Solution {
public int maxDepth(TreeNode root) {
return helper(root);
} private int helper(TreeNode cur_root) { if (cur_root == null)
return 0; return Math.max(helper(cur_root.left), helper(cur_root.right)) + 1;
}
}

The problem2:

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.

My analysis:

This problem is easy, but it differs a little with the previous problem: finding maximum path.
In this problem, we need to tackle with the "leaf node case" very carefully.
What's a leaf node?
A leaf node must has no left child or right child. We should mix this case with situation that a node has only one child. Some errors I have commited to solve the problem.
1. Directly return the minimum path among left-sub tree and right-sub tree. Apparently this is not right, what if a tree has only one left sub-tree with a length of 6?

return Math.min(left_min, right_min) + 1;

2. To fix the problem, I have tried to check if a node is a leaf node in each recursion.

private int helper(TreeNode cur_root) {
if (cur_root == null)
return 0;
if (cur_root.left == null && cur_root.right == null)
return 1;
...
return Math.min(left_min, right_min) + 1;
}

But this still does not work, because we must keep the checking condition "if (cur_root == null)". (if a node has only one child, the empty child must be tackled). Then the solution would aslo suffer from the wrong judgement as previous one.

Fix:
How about check if the current node has one child or two, then tackle and return the value respectively.
1. check if the current node with only one child.
Note: iff current node is a leaf node, the following stataments can still tackle it.

if (left_min == 0)
return right_min + 1; if (right_min == 0)
return left_min + 1;

2. if the current node with two children.

return Math.min(left_min, right_min) + 1;
public class Solution {
public int minDepth(TreeNode root) { if (root == null)
return 0; return helper(root);
} private int helper(TreeNode cur_root) {
if (cur_root == null)
return 0; int left_min = helper(cur_root.left);
int right_min = helper(cur_root.right); if (left_min == 0)
return right_min + 1; if (right_min == 0)
return left_min + 1; return Math.min(left_min, right_min) + 1;
}
}

[LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree的更多相关文章

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

  2. (二叉树 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 ...

  3. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

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

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

  5. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

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

  7. [LeetCode]题解(python):111 Minimum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...

  8. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

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

  9. 【一天一道LeetCode】#111. Minimum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. 如何学习一门新技术-iOS开发

    如何快速学习一门新技术 以CoreBluetooth 蓝牙开发为例.我们可以从官方获得的资源有:SampleCode, Documentation,API Reference. 先从Documenta ...

  2. 响应式WEB设计

    近期在学习有关响应式设计的内容,对此做了些整理,图片来源于网络,附上自己做的简单demo,没有js,只用CSS做了简单的搭建http://y.zhso.net/. 1.为什么需要响应式web设计 出于 ...

  3. C#调用百度云存储接口上传文件

    因前几日见园子里有人说可以把网站静态文件放在百度上,于是去百度开放平台看了看,发现之前那篇文章不是调的云存储接口啊... 于是自己写了个C#能调百度云存储的例子(百度云开放平台只提供php.java. ...

  4. postgre sql 字符串转为integer类型

    select cast(setting_value as integer) from ud_organization_setting. select cast('123123' as integer) ...

  5. Difference Between XML and XAML.

    XML, or Extensible Markup Language, is a subset  of the more complex SGML (Standard Generalized Mark ...

  6. JS调用android逻辑方法

    1.安卓打开webview时做如下配置 并做一回调接口 这里注意的是 参数 FULIBANG   和 回调接口方法  jsCallWebView 一会在JS里会用到 ================= ...

  7. 开发自己的cordova插件

    如果还没有配置过cordova环境,首先要下载nodejs,(下载地址https://nodejs.org/)下载完毕安装. 控制台: 1.输入npm -v 确定是否装上了 2.输入sudo npm ...

  8. 向RichTextBox控件不停的AppendText数据时,如何把光标的焦点始终显示到最后

    上面是csdn上的一个网友的问题,我的一个实现如下://让文本框获取焦点this.richTextBoxInfo.Focus();//设置光标的位置到文本尾this.richTextBoxInfo.S ...

  9. 【POJ1417】【带标记并查集+DP】True Liars

    Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was ...

  10. UILongPressGestureRecognizer的selector多次调用解决方法

    当你使用longPress gesture recognizer 时,你可能会发现调用了多次. UILongPressGestureRecognizer *longPress = [[UILongPr ...