Skip to content

Commit 969d7d3

Browse files
committed
https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/
1 parent 5fe27f5 commit 969d7d3

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ https://leetcode.cn/problems/basic-calculator/
574574

575575
https://leetcode.cn/problems/evaluate-reverse-polish-notation/
576576

577+
https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/
578+
577579
#### 安装教程
578580

579581
1. 安装`deno`
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
3+
export default function sumRootToLeaf(root: TreeNode | null, sum = 0): number {
4+
return !root
5+
? 0
6+
: (!root.left) && (!root.right)
7+
? sum * 2 + root.val
8+
: sumRootToLeaf(root.left, sum * 2 + root.val) +
9+
sumRootToLeaf(root.right, sum * 2 + root.val);
10+
}

0 commit comments

Comments
 (0)