leetCode-Record icon indicating copy to clipboard operation
leetCode-Record copied to clipboard

543二叉树的直径

Open fireairforce opened this issue 5 years ago • 0 comments

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
let max = 0;
var diameterOfBinaryTree = function(root) {
  let result = 0;
  depth(root);
  return result;
  function depth(root) {
    if (!root) {
      return 0;
    }
    let left = root.left ? depth(root.left) + 1 : 0;
    let right = root.right ? depth(root.right) + 1 : 0;
    result = Math.max(left + right, result);
    return Math.max(left, right);
  }
};

fireairforce avatar Mar 10 '20 08:03 fireairforce