-
Notifications
You must be signed in to change notification settings - Fork 43
/
DiameterOfBinaryTree.java
70 lines (60 loc) · 2.05 KB
/
DiameterOfBinaryTree.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
package LeetCodeJava.Tree;
import LeetCodeJava.DataStructure.TreeNode;
// https://leetcode.com/problems/diameter-of-binary-tree/
public class DiameterOfBinaryTree {
// V0
// IDEA : DFS
// V1
// IDEA : DFS
// https://leetcode.com/problems/diameter-of-binary-tree/editorial/
private int diameter;
public int diameterOfBinaryTree_2(TreeNode root) {
diameter = 0;
longestPath(root);
return diameter;
}
private int longestPath(TreeNode node){
if(node == null) return 0;
// recursively find the longest path in
// both left child and right child
int leftPath = longestPath(node.left);
int rightPath = longestPath(node.right);
// update the diameter if left_path plus right_path is larger
diameter = Math.max(diameter, leftPath + rightPath);
// return the longest one between left_path and right_path;
// remember to add 1 for the path connecting the node and its parent
return Math.max(leftPath, rightPath) + 1;
}
// V2
// IDEA : DFS
// https://leetcode.com/problems/diameter-of-binary-tree/solutions/3396281/solution/
int result = -1;
public int diameterOfBinaryTree_3(TreeNode root) {
dfs(root);
return result;
}
private int dfs(TreeNode current) {
if (current == null) {
return -1;
}
int left = 1 + dfs(current.left);
int right = 1 + dfs(current.right);
result = Math.max(result, (left + right));
return Math.max(left, right);
}
// V3
// IDEA: DFS
// https://leetcode.com/problems/diameter-of-binary-tree/solutions/3280141/beats-100-onlycode-in-java/
public int diameterOfBinaryTree_4(TreeNode root) {
int dia[] = new int[1];
ht(root,dia);
return dia[0];
}
public int ht(TreeNode root , int dia[]){
if(root == null) return 0;
int lh = ht(root.left,dia);
int rh = ht(root.right,dia);
dia[0] = Math.max(dia[0],lh+rh);
return 1 + Math.max(lh,rh);
}
}