-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBinaryTreeDepth.java
97 lines (94 loc) · 2.23 KB
/
BinaryTreeDepth.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package io.ziheng.codinginterviews;
import java.util.LinkedList;
import java.util.Queue;
/**
* 二叉树节点
*/
class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val) {
this.val = val;
this.left = null;;
this.right = null;;
}
}
/**
* 剑指 Offer 面试题 55-1:二叉树的深度
*
* 题目描述:
* 输入一颗二叉树的根节点,求该二叉树的深度。
*
* 知识点:["树"]
*/
public class BinaryTreeDepth {
public static void main(String[] args) {
BinaryTreeDepth obj = new BinaryTreeDepth();
TreeNode t1 = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
t1.left = t2;
t1.right = t3;
TreeNode root = t1;
System.out.println(
obj.binaryTreeDepth(root)
);
}
/**
* 剑指 Offer 面试题 55:二叉树的深度
*
* @param root
* @return int
*/
public int binaryTreeDepth(TreeNode root) {
// DFS
// return binaryTreeDepthDFS(root);
// BFS
return binaryTreeDepthBFS(root);
}
/**
* Depth First Search
*
* @param root
* @return int
*/
public int binaryTreeDepthDFS(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + Math.max(
binaryTreeDepth(root.left),
binaryTreeDepth(root.right)
);
}
/**
* Breath First Search
*
* @param root
* @return int
*/
private int binaryTreeDepthBFS(TreeNode root) {
if (root == null) {
return 0;
}
int treeDepth = 0;
Queue<TreeNode> aQueue = new LinkedList<>();
aQueue.offer(root);
while (!aQueue.isEmpty()) {
int size = aQueue.size();
for (int i = 0; i < size; i++) {
TreeNode node = aQueue.poll();
if (node.left != null) {
aQueue.offer(node.left);
}
if (node.right != null) {
aQueue.offer(node.right);
}
}
treeDepth++;
}
return treeDepth;
}
}
/* EOF */