Skip to content

Commit 1902e5d

Browse files
yanglbmezhaocchen
andcommitted
feat: add typescript solution to lc problem: No.0129
No.0129.Sum Root to Leaf Numbers Co-authored-by: zhaocchen <zhaocchen@163.com>
1 parent 973ee6a commit 1902e5d

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

solution/0100-0199/0129.Sum Root to Leaf Numbers/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,35 @@ class Solution {
126126
}
127127
```
128128

129+
### **TypeScript**
130+
131+
```ts
132+
/**
133+
* Definition for a binary tree node.
134+
* class TreeNode {
135+
* val: number
136+
* left: TreeNode | null
137+
* right: TreeNode | null
138+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
139+
* this.val = (val===undefined ? 0 : val)
140+
* this.left = (left===undefined ? null : left)
141+
* this.right = (right===undefined ? null : right)
142+
* }
143+
* }
144+
*/
145+
146+
function sumNumbers(root: TreeNode | null): number {
147+
return dfs(root);
148+
};
149+
150+
function dfs(root: TreeNode | null, preSum : number = 0): number {
151+
if (!root) return 0;
152+
preSum = preSum * 10 + root.val;
153+
if (!root.left && !root.right) return preSum;
154+
return dfs(root.left, preSum) + dfs(root.right, preSum);
155+
}
156+
```
157+
129158
### **C++**
130159

131160
```cpp

solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,35 @@ class Solution {
114114
}
115115
```
116116

117+
### **TypeScript**
118+
119+
```ts
120+
/**
121+
* Definition for a binary tree node.
122+
* class TreeNode {
123+
* val: number
124+
* left: TreeNode | null
125+
* right: TreeNode | null
126+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
127+
* this.val = (val===undefined ? 0 : val)
128+
* this.left = (left===undefined ? null : left)
129+
* this.right = (right===undefined ? null : right)
130+
* }
131+
* }
132+
*/
133+
134+
function sumNumbers(root: TreeNode | null): number {
135+
return dfs(root);
136+
};
137+
138+
function dfs(root: TreeNode | null, preSum : number = 0): number {
139+
if (!root) return 0;
140+
preSum = preSum * 10 + root.val;
141+
if (!root.left && !root.right) return preSum;
142+
return dfs(root.left, preSum) + dfs(root.right, preSum);
143+
}
144+
```
145+
117146
### **C++**
118147

119148
```cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
return dfs(root);
17+
};
18+
19+
function dfs(root: TreeNode | null, preSum : number = 0): number {
20+
if (!root) return 0;
21+
preSum = preSum * 10 + root.val;
22+
if (!root.left && !root.right) return preSum;
23+
return dfs(root.left, preSum) + dfs(root.right, preSum);
24+
}

0 commit comments

Comments
 (0)