-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiameterOfBinaryTree.java
55 lines (48 loc) · 2.07 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
package Algorithms.BinaryTrees;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 02 Feb 2025
*/
public class DiameterOfBinaryTree {
static class TreeNode {int val;TreeNode left, right;TreeNode() {}TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);
System.out.println("diameterOfBinaryTree(root): " + diameterOfBinaryTree(root));
}
public static int diameterOfBinaryTree(TreeNode root) {
if (root == null) return 0;
int leftDiameter = diameterOfBinaryTree(root.left);
int rightDiameter = diameterOfBinaryTree(root.right);
int leftHeight = height(root.left);
int rightHeight = height(root.right);
return Math.max(leftHeight + rightHeight, Math.max(leftDiameter, rightDiameter));
}
public static int height(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(height(root.left), height(root.right));
}
private int diameter;
public int diameterOfBinaryTree2(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;
}
}