输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度,根节点的深度视为 1 。
数据范围:节点的数量满足
,节点上的值满足 
进阶:空间复杂度
,时间复杂度 )
假如输入的用例为{1,2,3,4,5,#,6,#,#,7},那么如下图:
import java.util.Queue;
import java.util.LinkedList;
public class Solution {
public int TreeDepth(TreeNode pRoot)
{
if(pRoot == null){
return 0;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(pRoot);
int depth = 0, count = 0, nextCount = 1;
while(queue.size()!=0){
TreeNode top = queue.poll();
count++;
if(top.left != null){
queue.add(top.left);
}
if(top.right != null){
queue.add(top.right);
}
if(count == nextCount){
nextCount = queue.size();
count = 0;
depth++;
}
}
return depth;
}
}
import java.lang.Math;
public class Solution {
public int TreeDepth(TreeNode pRoot)
{
if(pRoot == null){
return 0;
}
int left = TreeDepth(pRoot.left);
int right = TreeDepth(pRoot.right);
return Math.max(left, right) + 1;
}
}
求树的深度,可以从层次遍历出发考虑 层次遍历可以使用队列完成,也可以使用递归完成,所以有两种方法 方法一:使用队列 class Solution: # 层次遍历 def levelOrder(self, root): # write your code here # 存储最后层次遍历的结果 res = [] # 层数 count = 0 # 如果根节点为空,则返回空列表 if root is None: return count # 模拟一个队列储存节点 q = [] # 首先将根节点入队 q.append(root) # 列表为空时,循环终止 while len(q) != 0: # 使用列表存储同层节点 tmp = [] # 记录同层节点的个数 length = len(q) for i in range(length): # 将同层节点依次出队 r = q.pop(0) if r.left is not None: # 非空左孩子入队 q.append(r.left) if r.right is not None: # 非空右孩子入队 q.append(r.right) tmp.append(r.val) if tmp: count += 1 # 统计层数 res.append(tmp) return count def TreeDepth(self, pRoot): # write code here # 使用层次遍历 # 当树为空直接返回0 if pRoot is None: return 0 count = self.levelOrder(pRoot) return count 方法二:使用递归方法 class Solution: def TreeDepth(self, pRoot): # write code here # 使用层次遍历 # 当树为空直接返回0 if pRoot is None: return 0 # 方法2:使用递归 # 如果该树只有一个结点,它的深度为1.如果根节点只有左子树没有右子树, # 那么树的深度为左子树的深度加1;同样,如果只有右子树没有左子树, # 那么树的深度为右子树的深度加1。如果既有左子树也有右子树, # 那该树的深度就是左子树和右子树的最大值加1. count = max(self.TreeDepth(pRoot.left), self.TreeDepth(pRoot.right)) + 1 return count
public class Solution {
public int TreeDepth(TreeNode pRoot)
{
return pRoot == null? 0 : Math.max(TreeDepth(pRoot.left),TreeDepth(pRoot.right)) + 1;
}
}
Python 非递归解法
class Solution:
# 层次遍历
def levelOrder(self, root):
# 统计层数
count = 0
# 如果根节点为空,则返回空列表
if root is None: return count
# 模拟一个队列储存节点
q = []
# 首先将根节点入队
q.append(root)
# 列表为空时,循环终止,说明没有新的叶子节点层
while len(q) != 0:
# 记录同层节点的个数
length = len(q)
for i in range(length):
# 将同层节点依次出队
r = q.pop(0)
if r.left is not None:
# 非空左孩子入队
q.append(r.left)
if r.right is not None:
# 非空右孩子入队
q.append(r.right)
count += 1
return count
def TreeDepth(self, pRoot):
# 使用层次遍历,当树为空直接返回0
if pRoot is None: return 0
return self.levelOrder(pRoot)
/*public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class Solution {
public int TreeDepth(TreeNode root) {
if(root==null) return 0;
if(root.left==null && root.right==null) return 1;
int left=0,right=0;
if(root.left!=null) left = TreeDepth(root.left);
if(root.right!=null) right = TreeDepth(root.right);
return left>right?left+1:right+1;
}
}
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
看到二叉树,递归基本没跑了,我们只要想好递归关系即可。我们假设已经正确拿到了root结点左右子树的最大深度,那么最后加一即可。
public class Solution {
public int TreeDepth(TreeNode root) {
//递归的出口,root为0则返回0,这里可以理解为root为0那肯定没有层数了
if(root == null){
return 0;
}
//拿到左子树的最大深度
int leftDep = TreeDepth(root.left);
//拿到右子树的最大深度
int rightDep = TreeDepth(root.right);
//找出最大值,并且加上root这一层即可
return Math.max(leftDep,rightDep) + 1;
}
}
import java.util.*;
public class Solution {
public int TreeDepth(TreeNode root) {
if(root==null) return 0;
ArrayList<TreeNode> arr=new ArrayList<>();
arr.add(root);
int count=0;
while(arr.size()!=0){
count++;
for(int i=0; i<arr.size(); i++){
TreeNode temp=arr.remove(0);
if(temp.left!=null)
arr.add(temp.left);
if(temp.right!=null)
arr.add(temp.right);
}
}
return count;
}
}
public class Solution {
public int TreeDepth(TreeNode root) {
if (root == null) {
return 0;
}
TreeNode cur = root;
return bs(cur);
}
public int bs(TreeNode head) {
if (head.left == null && head.right == null) {
return 1;
} else if (head.left != null && head.right == null) {
return 1 + bs(head.left);
} else if (head.left == null && head.right != null) {
return 1 + bs(head.right);
} else {
return 1 + Math.max(bs(head.left), bs(head.right));
}
}
}
//思路是层次遍历,通过插入特殊的结点new TreeNode(Integer.MIN_VALUE)来计算层次
public int TreeDepth(TreeNode root) {
int depth=0;
if(root!=null)
{
Queue q=new LinkedList<TreeNode>();
q.offer(root);
q.offer(new TreeNode(Integer.MIN_VALUE));
while(!q.isEmpty())
{
TreeNode tmp=(TreeNode)q.poll();
if(tmp.val!=Integer.MIN_VALUE)
{
if(tmp.left!=null)
q.offer(tmp.left);
if(tmp.right!=null)
q.offer(tmp.right);
}
else//当遇到特殊节点时,说明当前层已经遍历完了同时下一层的所有节点已经全部入队了,在每层的最后压入一个特殊节点
{
depth++;
if(!q.isEmpty())//重点,不然会死循环
q.offer(new TreeNode(Integer.MIN_VALUE));
}
}
}
return depth;
}