diameter of binary tree javascript
/**
* Calculate diameter with center `root`
* @param {TreeNode} root
* @returns {number}
*/
const countDiameter = root => {
if (!root) return 0;
return 1 + Math.max(countDiameter(root.left), countDiameter(root.right));
};
/**
* @param {TreeNode} root
* @returns {number}
*/
const diameterOfBinaryTree = root => {
if (!root) return 0;
const center = countDiameter(root.left) + countDiameter(root.right);
const left = diameterOfBinaryTree(root.left);
const right = diameterOfBinaryTree(root.right);
return Math.max(center, left, right);
};
Also in JavaScript:
- how to access global scope in to local scope using javascript examples
- jquery direct window print pdf
- fizzbuzz js
- javascript check table not empty
- express fingerprint
- get element inside object node.js
- how to remove an class in javascript
- JS get random number between
- deconstruction javascript check if exist attrib
- javascript get elements that exist in two arrays
- javascript span style
- jquery enable disable textbox
- ruby read json file
- Underscore.js
- javascript declare array
- array reverse algorithm in js
- substr() javascript
- how to use aos in react
- get count of class which is visible element
- adding a default option in switch statements
- javascript date
- sweetalert2 js
- boucle for in js
- validacao de campo vazio Toastr js