-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJz38.java
More file actions
70 lines (57 loc) · 1.46 KB
/
Jz38.java
File metadata and controls
70 lines (57 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.cpucode.java.simple;
import java.util.LinkedList;
import java.util.Queue;
/**
* 题目描述
* 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
* 示例1
* 输入
* {1,2,3,4,5,#,6,#,#,7}
* 返回值
* 4
*
* @author : cpucode
* @Date : 2021/1/23
* @Time : 11:52
* @Github : https://github.com/CPU-Code
* @CSDN : https://blog.csdn.net/qq_44226094
*/
public class Jz38 {
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public int TreeDepth(TreeNode root) {
if(root == null){
return 0;
}
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.add(root);
//深度
int depth = 0;
//当前遍历的数
int count = 0;
//当层遍历的总数
int nextcount = q.size();
while(q.size() > 0){
TreeNode node = q.poll();
if(node.left != null){
q.add(node.left);
}
if(node.right != null){
q.add(node.right);
}
count++;
if(count == nextcount){
depth++;
count = 0;
nextcount = q.size();
}
}
return depth;
}
}