递归的应用--求二叉树最大深度和最小深度

【求最大深度】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.

这里说的最大深度是指最深叶子节点到根节点的路径长度

<span style="font-size:18px;">	 public static int maxDepth(TreeNode root) {
		 if(root==null)return 0;
		 if(root.right==null && root.left==null)return 1;
		 else return 1+Math.max(maxDepth(root.right), maxDepth(root.left));
	    }</span>

【求最小深度】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.

这里说的最小深度是指最浅叶子节点到根节点的路径长度,求最小深度相对最大深度来讲要麻烦一点点。

<span style="font-size:18px;"> public static int minDepth(TreeNode root) {
		 if(root==null)return 0;
		 if(root.right==null && root.left==null)//没有子节点
		 {
			 return 1;
		 }
		 else if(root.right!=null && root.left!=null)//左右儿子都存在
		 {
			return Math.min( 1+minDepth(root.left),1+minDepth(root.right));

		 }
		 else if(root.right==null)//只有左儿子
		 {
			 return 1+minDepth(root.left);
		 }
		 else
		 {
			 return 1+minDepth(root.right);//只有右儿子
		 }
	 }</span>
时间: 2024-08-29 08:48:36

递归的应用--求二叉树最大深度和最小深度的相关文章

二叉树最大深度和最小深度

二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 如果二叉树为空,则深度为0 如果不为空,分别求左子树的深度和右子树的深度,去最大的再加1,因为根节点深度是1,要加进去. int maxDepth(TreeNode *root) { // write your code (here) if(root == NULL) return 0; int leftDepth = maxDepth(root->left); int rightDepth = ma

求二叉树的深度和宽度[Java]

这个是常见的对二叉树的操作.总结一下: 设节点的数据结构,如下: 1 class TreeNode { 2 char val; 3 TreeNode left = null; 4 TreeNode right = null; 5 6 TreeNode(char _val) { 7 this.val = _val; 8 } 9 } 1.二叉树深度 这个可以使用递归,分别求出左子树的深度.右子树的深度,两个深度的较大值+1即可. 1 // 获取最大深度 2 public static int get

递归求取二叉树最小深度和最大深度

public class Binarytreedept { /*  * 输出二叉树最小深度     * 核心思想:根节点到达最近的叶子节点的路径长度.  * 1.当根为空时,输出0.  * 2.当左子树为空时,输出右子树深度+1.  * 3.当右子树为空时,输出左子树深度+1.  * 4.以上条件都不满足时,输出min(左子树深度,右子树深度)+1.  *   * 输出二叉树最大深度   * 核心思想:根节点到达最远的叶子节点的路径长度.  * 1.如果二叉树为空,则深度为0;  * 2.如果不

求二叉树的最小深度

思路:用递归的方法求解. 输入:二叉树的根节点: 输出:二叉树的最小深度. 最小深度的定义:从根节点到叶子节点的最短路径上的节点数. 算法如下: 将二叉树分为这么几种情况: 传入的根节点为空,返回NULL: 传入根节点不为空,左子树为空,右子树为空,返回最小深度1: 传入根节点不为空,左子树为空,右子树不为空,返回右子树的最小深度+1: 传入根节点不为空,左子树不为空,右子树为空,返回左子树的最小深度+1: 传入根节点不为空,左右子树都不为空,则返回左右子树中最小深度的较小值+1. 代码如下:

【递归】Vijos P1132 求二叉树的先序序列(NOIP2001普及组第三题)

题目链接: https://vijos.org/p/1132 题目大意: 给定二叉树的中序和后序遍历,求该二叉树先序遍历. 题目思路: [递归] 这题妥妥递归. 二叉树先序根左右,中序左根右,后序左右根. 对于每一颗子树,它的后序最后一个必定是根,于是可以根据根在中序的位置把左子树和右子树区分开来. 1 // 2 //by coolxxx 3 // 4 #include<iostream> 5 #include<algorithm> 6 #include<string>

二叉树(5)----求二叉树节点数,递归与非递归

1.二叉树定义 typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTreeNode_t_ { BTreeNodeElement_t *m_pElemt; struct BTreeNode_t_ *m_pLeft; struct BTreeNode_t_ *m_pRight; } BTreeNode_t; 2.求二叉树节点数 对任意一个给定子树的节点数,左子树节点数+右子树节

二叉树(11)----求二叉树的镜像,递归和非递归方式

1.二叉树定义: typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTreeNode_t_ { BTreeNodeElement_t *m_pElemt; struct BTreeNode_t_ *m_pLeft; struct BTreeNode_t_ *m_pRight; } BTreeNode_t; 2.求二叉树镜像 比如: A                    

求二叉树镜像的递归非递归实现

1.二叉树定义: [cpp] view plain copy print? typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTreeNode_t_ { BTreeNodeElement_t     *m_pElemt; struct BTreeNode_t_    *m_pLeft; struct BTreeNode_t_    *m_pRight; } BTreeNod

二叉树(8)----求二叉树第K层的节点数和二叉树第K层的叶子节点数,递归方式

1.二叉树定义 typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTreeNode_t_ { BTreeNodeElement_t *m_pElemt; struct BTreeNode_t_ *m_pLeft; struct BTreeNode_t_ *m_pRight; } BTreeNode_t; 2.求二叉树第K层的节点数 (1)递归方式 给定根节点pRoot: 如