剑指offer55-Ⅰ 二叉树的深度【DFS】

题目描述

输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。

例如:

给定二叉树 [3,9,20,null,null,15,7]

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

返回它的最大深度 3 。

提示:

  1. 节点总数 <= 10000

解题思路

方法一:深度优先搜索

dfs方法,后序遍历,计算出左右子树的长度,递归。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return dfs(root);
}

private int dfs(TreeNode root) {
if(root==null){
return 0;
}
int leftLevel=dfs(root.left);
int rightLevel=dfs(root.right);
return Math.max(leftLevel,rightLevel)+1;
}


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

复杂度分析

时间复杂度:O(n)

空间复杂度:O(n) 递归栈

方法二:层次遍历

层次遍历,计算有多少层。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int res = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
res++;
}
return res;
}

复杂度分析

时间复杂度:O(n)

空间复杂度:O(n)

资料

剑指offer55-Ⅰ 二叉树的深度【DFS】

http://example.com/2021/05/31/剑指offer55-Ⅰ-二叉树的深度/

Author

John Doe

Posted on

2021-05-31

Updated on

2021-06-08

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.