Maximum Depth of Binary Tree,求树的最大深度

算法分析:求树的最小最大深度时候,都有两种方法,第一种是递归思想。树最大最小深度,即为它的子树的最大最小深度+1,是动态规划的思想。还有一种方法是层序遍历树,只不过求最小深度时,找到第一个叶子节点就可以返回,该节点的深度,即为树的最小深度。求最大深度时,需要层序遍历完整棵树。

public class MaximumDepthofBinaryTree
{
	public int maxDepth(TreeNode root)
	{
		if(root == null)
		{
			return 0;
		}
		int lmax = maxDepth(root.left);
		int rmax = maxDepth(root.right);
		if(lmax == 0 && rmax == 0)
		{
			return 1;
		}
		if(lmax == 0)
		{
			return rmax + 1;
		}
		if(rmax == 0)
		{
			return lmax + 1;
		}
		return Math.max(lmax, rmax) + 1;
	}

	public int maxDepth2(TreeNode root)
	{
		if(root == null)
		{
			return 0;
		}
		ArrayList<TreeNode> list = new ArrayList<>();
		list.add(root);
		int count = 1;
		while(!list.isEmpty())
		{
			ArrayList<TreeNode> temp = new ArrayList<>();
			for (TreeNode node : list) {
				if(node.left != null)
				{
					temp.add(node.left);
				}
				if(node.right != null)
				{
					temp.add(node.right);
				}
			}
			count ++;
			list = temp;
		}
		return count;
	}
}

  

时间: 2024-10-01 04:27:51

Maximum Depth of Binary Tree,求树的最大深度的相关文章

LeetCode Maximum Depth of Binary Tree (求树的深度)

题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 *

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 longest path from the root node down to the farthest leaf node. 分析:求二叉树的最大深度 解法一:很容易想到的便是递归(深度优先搜索) (1)如果根节点是空,则返回0:否则转到(2) (2)  l = 左子树的最大深度; r = 右子树的最大深

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 longest path from the root node down to the farthest leaf node. 题目标签:Tree 这道题目给了我们一个二叉树,要我们找到最大深度,就是从root点到最深的那个点之间点的数量.利用post order 来遍历二叉树,对于每一个点,它的两个chi

Leetcode 树 Maximum Depth of Binary Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Maximum Depth of Binary Tree Total Accepted: 16605 Total Submissions: 38287 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the ro

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution 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

学习笔记:LeetCode104:Maximum Depth of Binary Tree

LeetCode104:Maximum Depth of Binary Tree 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. 求二叉树的最大深度,树节点包含当前值及分别指向其左右子节点的引用,如下: 1 /** 2 * De

34: Maximum Depth of Binary Tree

/************************************************************************/        /*       34:     Maximum Depth of Binary Tree                                        */        /************************************************************************

【leetcode刷题笔记】Maximum Depth of Binary Tree

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. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

LeetCode——Maximum Depth of Binary Tree

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. 此题和求二叉树的最短路径几乎一模一样. public int maxDepth(TreeNode root) { if (root == null) return 0; int a

我也来刷LeetCode——0、Maximum Depth of Binary Tree(开篇)

作为一个非科班出身的程序员,还是很有必要刷刷题的.之前有个大牛说,菜鸟刷题可以从AC率高的刷起,这样可以快速培养信心.将LeetCode的题目按照AC率从高到低排序,第一道题目为 [Maximum Depth of Binary Tree]. 从题目就可以看出,是求二叉树的最大深度.深度的概念,是指从根节点到叶子节点的这条路径上的总节点数. 要求二叉树的最大深度,只需要求出左子树的深度和右子树的深度,取两者最大值再加 1 ,即为二叉树的最大深度. 若该二叉树为空,返回 0 . 然后就是将算法翻译