-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path543.java
More file actions
60 lines (59 loc) · 1.77 KB
/
543.java
File metadata and controls
60 lines (59 loc) · 1.77 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
__________________________________________________________________________________________________
sample 0 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int diameterOfBinaryTree(TreeNode root) {
int[] max = new int[1];
dfs(root, max);
return max[0];
}
private int dfs(TreeNode node, int[] max) {
if (node == null) return 0;
int l = dfs(node.left, max);
int r = dfs(node.right, max);
max[0] = Math.max(max[0], l + r);
return Math.max(l, r) + 1;
}
}
__________________________________________________________________________________________________
sample 37108 kb submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int diameterOfBinaryTree(TreeNode root) {
int[] ans = new int[1];
dfsHelper(root, ans);
return ans[0];
}
private int dfsHelper(TreeNode root, int[] ans) {
if (root == null) return -1;
int left = dfsHelper(root.left, ans);
int right = dfsHelper(root.right, ans);
if (left == -1 && right == -1) {
return 0;
} else if (left == -1) {
ans[0] = Math.max(ans[0], 1 + right);
} else if (right == -1) {
ans[0] = Math.max(ans[0], 1 + left);
} else {
ans[0] = Math.max(ans[0], 1 + left + 1 + right);
}
return 1 + Math.max(left, right);
}
}
__________________________________________________________________________________________________