author: Blankj blog : http://blankj.com time : 2017/10/13 desc :
| 12 | * </pre> |
| 13 | */ |
| 14 | public 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected