deno.land / x / masx200_leetcode_test@10.6.5 / count-complete-tree-nodes / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
export default function countNodes( root: TreeNode | null, leftDepth?: number, rightDepth?: number,): number { if (root === null) return 0; let left = 0, right = 0; let curNode: TreeNode | null = root; if (typeof leftDepth !== "undefined") { left = leftDepth; } else { while (curNode !== null) { left++; curNode = curNode.left; } }
curNode = root; if (typeof rightDepth !== "undefined") { right = rightDepth; } else { while (curNode !== null) { right++; curNode = curNode.right; } } if (left === right) { return 2 ** left - 1; } return ( 1 + countNodes(root.left, left - 1) + countNodes(root.right, undefined, right - 1) );}
masx200_leetcode_test

Version Info

Tagged at
a year ago