-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNodeWithLargestData.java
More file actions
23 lines (19 loc) · 900 Bytes
/
NodeWithLargestData.java
File metadata and controls
23 lines (19 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package binaryTrees1;
import static binaryTrees1.BinaryTreeInput.takeTreeInputDetailed;
import static binaryTrees1.BinaryTreeInput.printBinaryTree;
public class NodeWithLargestData {
public static int nodeWithLargestData(BinaryTreeNode<Integer> root) {
if (root == null) return -1;
int largestLeft = nodeWithLargestData(root.left);
int largestRight = nodeWithLargestData(root.right);
// comparing three number which one is maximum
return Math.max(root.data, Math.max(largestLeft, largestRight));
}
public static void main(String[] args) {
var root = takeTreeInputDetailed(true, 0, true);
System.out.println("----------The Tree is----------");
printBinaryTree(root);
System.out.println("-------------------------------");
System.out.println("Node with Largest Data: " + nodeWithLargestData(root));
}
}