-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiameterOfBinaryTree.java
More file actions
42 lines (31 loc) · 1.21 KB
/
Copy pathDiameterOfBinaryTree.java
File metadata and controls
42 lines (31 loc) · 1.21 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
// Given a binary tree, you need to compute the length of the diameter of the tree.
// The diameter of a binary tree is the length of the longest path between
// any two nodes in a tree.
// This path may or may not pass through the root.
// See: https://leetcode.com/problems/diameter-of-binary-tree/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.initTree;
import leetcode.util.tree.TreeNode;
public class DiameterOfBinaryTree {
private int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
helper(root);
return max;
}
private int helper(TreeNode root) {
if (root == null) {
return 0;
}
int left = helper(root.left);
int right = helper(root.right);
max = Math.max(max, left + right);
return Math.max(left + 1, right + 1);
}
public static void main(String[] args) {
TreeNode t1 = initTree(1, 2, 3, 4, 5, null, null, null, 10, 6, null, null, null, 8);
System.out.println(new DiameterOfBinaryTree()
.diameterOfBinaryTree(t1));
System.out.println(new DiameterOfBinaryTree()
.diameterOfBinaryTree(initTree(1)));
}
}