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. 【nodejs】创建udp套接字的类型参数的含义

    nodejs在创建udp套接字的时候,需要传入一个类型参数.有两种类型参数可供选择:udp4和udp6.udp4对应的就是ipv4,udp6对应的是ipv6.

  2. Eclipse3.7默认字体修改-找回Courser-New字体

    1.找到jFace并用WinRAR打开之: jFace的具体位置:$Eclipse目录$/plugins/org.eclipse.jface_3.7.0.I20110522-1430.jar,找到后, ...

  3. Java基础知识强化之集合框架笔记25:Vector的特有功能

    1. Vector的特有功能: (1)添加功能         public void addElement(Object obj)       -- add() (2)获取功能         pu ...

  4. 实现一个线程安全的Queue队列

    使用装饰者模式实现一个线程安全的Queue队列. public class SynchronizedQueue<E> implements Queue<E>, Serializ ...

  5. c# 二维码 显示

    需要引用 ThoughtWorks.QRCode.dll 网上可下载 //方法 public ActionResult GenerateQRCode(string content) { try { _ ...

  6. c# 操作.config中AppSettings配置节

    ConfigurationSettings.AppSettings[key].ToString(); 这种方式很眼熟吧? 不过这种方式基本过时了,虽然还能用. 微软建议采用ConfigurationM ...

  7. android 如何解决模块之间的通讯的耦合问题

    使用EventBus http://wuyexiong.github.io/blog/2013/04/30/android-fragment/ http://yunfeng.sinaapp.com/? ...

  8. GoogleAuthenticator

    <?php /** * PHP Class for handling Google Authenticator 2-factor authentication * * @author Micha ...

  9. X3850 Linux 下DSA日志收集办法

    收集工具下载 RHEL 6: 32bit-- [IBM 下载]http://delivery04.dhe.ibm.com/sar/CMA/XSA/03tza/1/ibm_utl_dsa_dsytb7x ...

  10. Vim简明教程【CoolShell】

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...