解题思路:
class Solution {int ans;//记录节点数public int diameterOfBinaryTree(TreeNode root) {ans = 1;depth(root);return ans - 1;//节点数减 1 就是路径长度}public int depth(TreeNode root) {if (root == null) return 0;int l = depth(root.left);int r = depth(root.right);ans = Math.max(ans, l + r + 1);return Math.max(l, r) + 1;}
}