Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 95c2db4

Browse files
update: 129
1 parent 9ddaf94 commit 95c2db4

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
8888
| 123 | [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) <sup>*</sup> | [JavaScript](./src/best-time-to-buy-and-sell-stock-iii/res.js) | Hard |
8989
| 127 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [JavaScript](./src/word-ladder/res.js) | Medium |
9090
| 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) | [JavaScript](./src/longest-consecutive-sequence/res.js) | Medium |
91+
| 129 | [sum-root-to-leaf-numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) | [TypeScript](./src/sum-root-to-leaf-numbers/res.ts) | Medium |
9192
| 130 | [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [JavaScript](./src/surrounded-regions/res.js) | Medium |
9293
| 131 | [palindrome-partitioning](https://leetcode.com/problems/palindrome-partitioning/) | [TypeScript](./src/palindrome-partitioning/res.ts) | Medium |
9394
| 133 | [Clone Graph](https://leetcode.com/problems/clone-graph/) | [JavaScript](./src/clone-graph/res.js) | Medium |

‎src/sum-root-to-leaf-numbers/res.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function sumNumbers(root: TreeNode | null): number {
16+
const result: number[][] = [];
17+
18+
const dfs = (root: TreeNode | null, nums: number[]) => {
19+
if (!root) {
20+
return ;
21+
}
22+
23+
if (!root.left && !root.right) {
24+
result.push([...nums, root.val]);
25+
return ;
26+
}
27+
28+
dfs(root.left, [...nums, root.val]);
29+
dfs(root.right, [...nums, root.val]);
30+
}
31+
32+
dfs(root, []);
33+
34+
return result.map(item => parseInt(item.join(''))).reduce((a, b) => a + b);
35+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /