File tree 3 files changed +82
-0
lines changed
solution/0100-0199/0129.Sum Root to Leaf Numbers
3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change @@ -126,6 +126,35 @@ class Solution {
126
126
}
127
127
```
128
128
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
+
129
158
### ** C++**
130
159
131
160
``` cpp
Original file line number Diff line number Diff line change @@ -114,6 +114,35 @@ class Solution {
114
114
}
115
115
```
116
116
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
+
117
146
### ** C++**
118
147
119
148
``` cpp
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments