maximum-depth-of-binary-tree&&minimum-depth-of-binary-tree

关于二叉树,目前在leetcode中遇到了两道题目,一道是二叉树的最大深度,一道是二叉树的最小深度;关于二叉树,整好借此机会再学习一下;

二叉树是一种特殊的树,在二叉树中每个节点最多有两个子节点,一般称为左子节点和右子节点(或左孩子和右孩子),并且二叉树的子树有左右之分,其次序不能任意颠倒。二叉树是递归定义的,因此,与二叉树有关的题目基本都可以用递归思想解决。

第一道题目:

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的距离

样例

给出一棵如下的二叉树:

  1
 / \
2   3
   /   4   5

这个二叉树的最大深度为3.

这道题用递归的思路来解决的话,但是为了保证我们得到的是最大深度,应该最初的root左右子节点分开递归最后得到最大的, 代码还是非常简单的。

本质还是深度优先搜索;

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     /**
14      * @param root: The root of binary tree.
15      * @return: An integer.
16      */
17     public int maxDepth(TreeNode root) {
18         // write your code here
19         int leftDepth;
20         int rightDepth;
21         if(root == null ){
22             return 0;
23         }
24         else{
25             leftDepth = maxDepth(root.left)+1;
26             rightDepth = maxDepth(root.right)+1;
27             return Math.max(leftDepth,rightDepth);
28         }
29
30     }
31 }

第二道题目:

给定一个二叉树,找出其最小深度。

二叉树的最小深度为根节点到最近叶子节点的距离。

样例

给出一棵如下的二叉树:

1

/     \

2       3

/    \

4      5

这个二叉树的最小深度为 2.

最开始看这道题的时候,其实很容易就想到这不是很简单吗?把最大深度的返回值改为两个里面最小的就可以。但其实并不是这样的。因为如果二叉树中出现了单个子节点为空的情况,返回值就会为0,但这个子节点并不是叶节点。

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     /**
14      * @param root: The root of binary tree.
15      * @return: An integer.
16      */
17     public int minDepth(TreeNode root) {
18         // write your code here
19         if(root == null )    return 0;
20         int leftDepth = minDepth(root.left);
21         int rightDepth = minDepth(root.right);
22         if(leftDepth==0||rightDepth ==0){//主要是对左右子节点是否有单个为空的情况进行判断
23             return 1+rightDepth + leftDepth;
24         }  else {
25             return 1+Math.min(rightDepth,leftDepth);
26         }
27     }
28 }
时间: 2024-10-21 16:04:16

maximum-depth-of-binary-tree&&minimum-depth-of-binary-tree的相关文章

[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 inc

33. Minimum Depth of Binary Tree && Balanced Binary Tree

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

leetcode笔记: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. 二. 题目分析 这道题属于二叉树的深度优先搜索,然后返回深度最小的值,可以递归(当然,也可以使用迭代)来实现.递归退出的条件是到达叶子节点或者到达空子树,使用空子树

[LeetCode] 111. Minimum Depth of Binary Tree Java

题目: 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. 题意及分析:求一棵二叉树的最低高度,即根节点到叶子节点的最短路径.遍历二叉树,然后用一个变量保存遍历到当前节点的最小路径即可,然后每次遇到叶子节点,就判断当前叶节点高度和先

Minimum Depth of Binary Tree

1. Title 2. Http address https://leetcode.com/problems/minimum-depth-of-binary-tree/ 3. The question 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

111. Minimum Depth of Binary Tree java solutions

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. Subscribe to see which companies asked this question Show Tags Show Similar Problems 1 /**

35: Minimum Depth of Binary Tree

/************************************************************************/        /*       35:     Minimum Depth of Binary Tree                                         */        /***********************************************************************

Java [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 shortest path from the root node down to the nearest leaf node. 解题思路: 递归. 代码描述: /** * Definition for a binary tree node. * public class TreeNode { *

Java for 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 shortest path from the root node down to the nearest leaf node. 解题思路:注意minimum depth最后遍历的那个点,left right都必须为null,JAVA实现如下: public int minDepth(TreeNode roo

Minimum Depth of Binary Tree leetcode java

题目: 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. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public