题目描述
思路
所谓二叉树的直径,就是左右子树的最大深度之和。
方法一:
/**
* 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 {
private int res = 0;
public int diameterOfBinaryTree(TreeNode root) {
postOrder(root);
return res;
}
private int postOrder(TreeNode node) {
if (node == null) return 0;
int leftMaxDepth = postOrder(node.left);
int rightMaxDepth = postOrder(node.right);
res = Math.max(res, leftMaxDepth + rightMaxDepth);
return Math.max(leftMaxDepth, rightMaxDepth) + 1;
}
}
标签:node,right,TreeNode,LeetCode543,val,int,二叉树,LeetCode,left
From: https://www.cnblogs.com/keyongkang/p/17931117.html