一:题目
二:上码
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {/**- 分别求取左右子树的最大高度 求取其和 */ int ans = 0;public int getLenNode(TreeNode root) {if (root == null) return 0;int leftLen = getLenNode(root.left);int rightLen = getLenNode(root.right);int depth = Math.max(leftLen,rightLen) + 1;ans = Math.max(leftLen+rightLen,ans);return depth;}public int diameterOfBinaryTree(TreeNode root) {getLenNode(root);return ans;}
}