title: Is anyone familiar with the "Diameter of a Binary Tree" question on Leetcode?
body: <p>I am having a hard time understanding the &quot;Diameter of a Tree&quot; question on leetcode, and though I was able to see the solution it's no use if I won't be able to come up with one on my own since I don't understand the question, to begin with. I ask if anyone could please explain step by step how this problem is approached, I would greatly appreciate it.<br />
  Here is the question:</p>
  <p>Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.</p>
  <p>Example:
  Given a binary tree</p>
  <pre><code>      1
       / \
      2   3
     / \     
    4   5    
  </code></pre>
  <p>Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].</p>
  <p>Note: The length of path between two nodes is represented by the number of edges between them.</p>
  <p>Here is the solution:</p>
  <pre><code>class Solution {
      int ans;
      public int diameterOfBinaryTree(TreeNode root) {
          ans = 0;
          depth(root);
          return ans;
      }
      public int depth(TreeNode node) {
          if (node == null) return 0;
          int L = depth(node.left);
          int R = depth(node.right);
          ans = Math.max(ans, L + R);
          return 1 + Math.max(L, R);
      }
  }
  
  </code></pre>