package leetcode;

/**
 * https://leetcode.com/problems/diameter-of-binary-tree/
 */
public class Problem543 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public int diameterOfBinaryTree(TreeNode root) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem543 prob = new Problem543();

        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);

        System.out.println(prob.diameterOfBinaryTree(root));
    }
}

@@ -260,16 +260,122 @@
        %7D
+%0A%0A        @Override%0A        public String toString() %7B%0A            return Integer.toString(val);%0A        %7D
 %0A    %7D%0A%0A
@@ -439,15 +439,69 @@
     
-// TODO
+Max max = new Max();%0A        diameterOfBinaryTree(root, max);
 %0A   
@@ -516,9 +516,15 @@
 urn 
-0
+max.max
 ;%0A  
@@ -533,29 +533,30 @@
 %7D%0A%0A    p
-ublic
+rivate
  static 
 void mai
@@ -551,290 +551,323 @@
 tic 
-void main(String%5B%5D args) %7B%0A        Problem543 prob = new Problem543();%0A
+class Max %7B%0A        int max;
 %0A    
+%7D%0A%0A
     
-TreeNode root = new TreeNode(1);%0A        root.left = new TreeNode(2);%0A        root.right = new TreeNode(3);%0A        root.left
+private static int diameterOfBinaryTree(TreeNode node, Max max) %7B%0A        if (node == null) %7B%0A            return 0;%0A        %7D%0A        if (node
 .left =
+=
  n
-ew TreeNode(4);%0A        root.left.right = new 
+ull && node.right == null) %7B%0A            return 1;%0A        %7D%0A        int a = diameterOfBinary
 Tree
-Node(5
+(node.left, max
 );%0A
-%0A
         
 Syst
@@ -866,59 +866,132 @@
     
-System.out.println(prob.diameterOfBinaryTree(root))
+int b = diameterOfBinaryTree(node.right, max);%0A        max.max = Math.max(max.max, a + b);%0A        return Math.max(a, b) + 1
 ;%0A  

Solve problem 543