Skip to content

Commit f4d3a0f

Browse files
authored
Update 662-maximum-width-of-binary-tree.js
1 parent 0f9582e commit f4d3a0f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

662-maximum-width-of-binary-tree.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
const widthOfBinaryTree = function (root) {
14+
let max = 1n
15+
const stack = []
16+
const bi = BigInt
17+
const width = (root, level, pos) => {
18+
if (root == null) return
19+
if (level >= stack.length) stack.push(pos)
20+
else {
21+
// console.log(stack)
22+
const tmp = pos - stack[level] + 1n
23+
if(tmp > max) max = tmp
24+
}
25+
width(root.left, level + 1, 2n * pos)
26+
width(root.right, level + 1, 2n * pos + 1n)
27+
}
28+
width(root, 0, 1n)
29+
return Number(max)
30+
}
31+
32+
33+
// another
34+
135
/**
236
* Definition for a binary tree node.
337
* function TreeNode(val) {

0 commit comments

Comments
 (0)