fork download
  1. /*
  2.  
  3. Following is the structure used to represent the Binary Tree Node
  4.  
  5. class BinaryTreeNode<T> {
  6. T data;
  7. BinaryTreeNode<T> left;
  8. BinaryTreeNode<T> right;
  9.  
  10. public BinaryTreeNode(T data) {
  11. this.data = data;
  12. this.left = null;
  13. this.right = null;
  14. }
  15. }
  16.  
  17. */
  18.  
  19.  
  20. class Pair {
  21.  
  22. int ht;
  23. int diameter;
  24. }
  25.  
  26.  
  27. public class Solution {
  28.  
  29. public static int diameterOfBinaryTree(BinaryTreeNode<Integer> root){
  30.  
  31. Pair ans = diameterOfBinaryTree_(root);
  32.  
  33. return ans.diameter;
  34. }
  35.  
  36.  
  37. public static Pair diameterOfBinaryTree_(BinaryTreeNode<Integer> root){
  38.  
  39. if (root == null) {
  40. Pair base = new Pair();
  41.  
  42. base.ht = 0;
  43. base.diameter = 0;
  44.  
  45. return base;
  46. }
  47.  
  48.  
  49. Pair left = diameterOfBinaryTree_(root.left);
  50. Pair right = diameterOfBinaryTree_(root.right);
  51.  
  52. Pair myAns = new Pair();
  53.  
  54. myAns.ht = Math.max(left.ht, right.ht) + 1;
  55.  
  56. int opt1 = left.diameter;
  57.  
  58. int opt2 = right.diameter;
  59.  
  60. int opt3 = left.ht + right.ht + 1;
  61.  
  62. myAns.diameter = Math.max(opt1, Math.max(opt2, opt3));
  63.  
  64. return myAns;
  65. }
  66.  
  67.  
  68.  
  69. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:27: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
       ^
Main.java:29: error: cannot find symbol
	public static int diameterOfBinaryTree(BinaryTreeNode<Integer> root){
	                                       ^
  symbol:   class BinaryTreeNode
  location: class Solution
Main.java:37: error: cannot find symbol
	public static Pair diameterOfBinaryTree_(BinaryTreeNode<Integer> root){
	                                         ^
  symbol:   class BinaryTreeNode
  location: class Solution
3 errors
stdout
Standard output is empty