-
Notifications
You must be signed in to change notification settings - Fork 12
/
Problem_6_Tree_Diameter.java
35 lines (29 loc) · 990 Bytes
/
Problem_6_Tree_Diameter.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
package Tree_Depth_First_Search;
// Problem Statement: Tree Diameter (medium)
// LeetCode Question: 543. Diameter of Binary Tree
public class Problem_6_Tree_Diameter {
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode (int x) {
this.val = x;
}
}
private int treeDiameter = 0;
public int findDiameter(TreeNode root){
treeDiameter = 0;
calculateHeight(root);
return treeDiameter;
}
public int calculateHeight(TreeNode currentNode){
if (currentNode == null) return 0;
int leftTreeHeight = calculateHeight(currentNode.left);
int rightTreeHeight = calculateHeight(currentNode.right);
if (leftTreeHeight != 0 && rightTreeHeight != 0) {
int diameter = leftTreeHeight + rightTreeHeight + 1;
treeDiameter = Math.max(treeDiameter, diameter);
}
return Math.max(leftTreeHeight, rightTreeHeight) + 1;
}
}