543. Diameter of Binary Tree#

 1class Solution {
 2  int diameter = 0;
 3
 4  public int diameterOfBinaryTree(TreeNode root) {
 5    helper(root);
 6    return diameter;
 7  }
 8
 9  private int helper(TreeNode node) {
10    if (node == null) { return 0; }
11
12    int left = helper(node.left);
13    int right = helper(node.right);
14
15    // update diameter if it has max from current node
16    diameter = Math.max(diameter, (left + right));
17
18    // returning for higher-level
19    return Math.max(left, right) + 1;
20  }
21}