/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
// const diameterOfBinaryTree = function (root) {
// const value = { height: 0, max: 0 }
// inorderTraversal(root, value);
// return value.max;
// };
// const inorderTraversal = function (root, value) {
// if (root === null) {
// return 0;
// }
// const letfHeight = inorderTraversal(root.right, value);
// const rightHeight = inorderTraversal(root.left, value);
// value.max = Math.max(value.max, letfHeight + rightHeight);
// value.height = (letfHeight > rightHeight ? letfHeight : rightHeight) + 1;
// return value.height;
// };
const diameterOfBinaryTree = function (root) {
let value = { max: 0 }
inorderTraversal(root, value);
return value.max;
};
const inorderTraversal = function (root, value) {
if (root === null) {
return 0;
}
const letfHeight = inorderTraversal(root.right, value);
const rightHeight = inorderTraversal(root.left, value);
value.max = Math.max(value.max, letfHeight + rightHeight);
return (letfHeight > rightHeight ? letfHeight : rightHeight) + 1;
};