MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0543/Solution.java:14–34  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/13 desc :

Source from the content-addressed store, hash-verified

12 * </pre>
13 */
14public class Solution {
15 int max = 0;
16
17 public int diameterOfBinaryTree(TreeNode root) {
18 helper(root);
19 return max;
20 }
21
22 private int helper(TreeNode root) {
23 if (root == null) return 0;
24 int l = helper(root.left);
25 int r = helper(root.right);
26 if (l + r > max) max = l + r;
27 return Math.max(l, r) + 1;
28 }
29
30 public static void main(String[] args) {
31 Solution solution = new Solution();
32 System.out.println(solution.diameterOfBinaryTree(TreeNode.createTestData("[1,2,3,4,5]")));
33 }
34}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected