[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 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的更多相关文章
- [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 ...
- (二叉树 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 ...
- [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 ...
- 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 ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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 ...
- [LeetCode]题解(python):111 Minimum Depth of Binary Tree
题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...
- 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 ...
- 【一天一道LeetCode】#111. Minimum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- java Map实现的cache manager
一个模仿memcached的JAVA虚拟缓存工具,可以缓存java对象 import java.io.ByteArrayInputStream; import java.io.ByteArrayOut ...
- eclipse 常见问题及解决
1. Target runtime Apache Tomcat v6.0 is not defined.错误解决方法 原文:http://blog.csdn.net/xw13106209/articl ...
- java Permissions and Security Policy--官方文档
3 Permissions and Security Policy 3.1 The Permission Classes The permission classes represent access ...
- poj 1821 Fence 单调队列优化dp
/* poj 1821 n*n*m 暴力*/ #include<iostream> #include<cstdio> #include<cstring> #incl ...
- a标签的背景图在ie8下显示问题
今天遇到个小问题,纠结了很久,分享下 a标签添加背景图,需要给a添加display:block样式 但是在ie8下还是不能显示背景图,开始以为是由于a标签为空造成的,试了下添加内容也没用,后来注意到一 ...
- java的各个队列之间的联系和区别是什么
java的各个并发队列之间的联系和区别 java.util.concurrent是在并发编程中很常用的实用工具类 ArrayBlockingQueue, DelayQueue, LinkedBlock ...
- cookie防篡改
概述: 除了 session 外,一般不会在客户端的 cookies 里保存过于重要的凭据,但电商应用有时候不可避免地存储了一些敏感数据到客户端,当然不希望被篡改. 目的: 让服务器端能识别cooki ...
- Log4j配置的经典总结,打印日志文件,日志存库
一.介绍 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制 日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器.NT的事件记录器.UNIX Sy ...
- 设置css三种方法的优先级
有的小伙伴问了,如果有一种情况:对于同一个元素我们同时用了三种方法设置css样式,那么哪种方法真正有效呢?在下面代码中就出现了这种情况 1.使用内联式CSS设置“超酷的互联网”文字为粉色. 2.然后使 ...
- linux定时器
我们常常有设置系统在某一时间执行相应动作的需求,比如设置电脑什么时候自动锁屏,什么时候自动关机,设置应用程序什么时候自动运行,什么时候自动退出.这些与时间相关的功能,都需要依靠操作系统中的定时器来实现 ...