leecode/backlog/543.diameter-of-binary-tree.js
2020-05-22 18:17:19 +08:00

37 lines
865 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @lc app=leetcode id=543 lang=javascript
*
* [543] Diameter of Binary Tree
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
function maxDepth(root, res) {
if (root === null) return 0;
const l = maxDepth(root.left, res);
const r = maxDepth(root.right, res);
res.val = Math.max(res.val, l + r);
return 1 + Math.max(l, r);
}
/**
* @param {TreeNode} root
* @return {number}
*/
var diameterOfBinaryTree = function(root) {
// 如果不计算max 直接1+ Math.max(maxLeft, maxRight) 得到的结果实际上是经过root节点的最大值并不一定是总体最大值
// 题目也做了说明, ”最大值不一定经过root“
if (root === null) return 0;
const res = {
val: 0
};
maxDepth(root, res);
return res.val;
};