[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 ...
随机推荐
- IOS—通过ChildViewController实现view的切换
IOS-通过ChildViewController实现view的切换 在以前,一个UIViewController的View可能有很多小的子view.这些子view很多时候被盖在最后,我们在最外层Vi ...
- linux下vi命令笔记
vim 编辑器 全屏编辑器 模式化编辑器 vi:Visual Interfacevim:VI iMproved vi增强版vi模式: 编辑模式(命令模式)(默认处于编辑模式) Ct ...
- ASP.NET MVC 第三回 Controller与View
这节我们让ASP.NET MVC真正的跑起来 一.新建Controller 首先我们自己新建一个新的Controller在Controllers上点右键,添加,Controller选项 之后出现一 ...
- 高性能动画!HTML5 Canvas JavaScript框架KineticJS
高性能动画!HTML5 Canvas JavaScript框架KineticJS KineticJS是一款开源的HTML5 Canvas JavaScript框架,能为桌面和移动应用提供高性能动画,并 ...
- jQuery失去焦点的时候注册验证
//注册验证$('form :input').blur(function () { if ($("#txtName").val() == "") { $(&qu ...
- <input value="hidden">的作用
这样子上个页面的参数可以保存在这个页面,为下个页面获取参数做准备,在这个页面表单提交的时候下个页面可以获取参数
- Eclipse清除SVN密码
1. Eclipse/Subclipse并不会记录用户名及密码, SVN密码是由SVN客户端(JavaHL or SVNKit)负责维持的 2. 可查看Eclipse的Preferences中SVN属 ...
- 利用bat批量执行脚本文件
1.读取目录文件 利用bat 的for命令读取中的sql文件 for /r %%c in (0*.sql) do echo %%c %%c 相当于变量 in() 中的为循环的范围 此句的作用是显示当前 ...
- mysql - 编码
show variables like 'character%'; 通过以上命令可以查询编码情况, 不过,在安装的时候,建议选择‘gbk’这类中文编码, 如果选择的是utf8,则在处理的过程中需要进行 ...
- 进程识别号(PID)的理解
PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...