Skip to content

Commit a2b4cb0

Browse files
committed
feat: add typescript solution to lc problem: No.0144.Binary Tree Preorder Traversal
1 parent 5e2cc9e commit a2b4cb0

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

solution/0100-0199/0144.Binary Tree Preorder Traversal/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,40 @@ class Solution {
296296
}
297297
```
298298

299+
### **TypeScript**
300+
301+
```ts
302+
/**
303+
* Definition for a binary tree node.
304+
* class TreeNode {
305+
* val: number
306+
* left: TreeNode | null
307+
* right: TreeNode | null
308+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
309+
* this.val = (val===undefined ? 0 : val)
310+
* this.left = (left===undefined ? null : left)
311+
* this.right = (right===undefined ? null : right)
312+
* }
313+
* }
314+
*/
315+
316+
function preorderTraversal(root: TreeNode | null): number[] {
317+
if (root == null) return [];
318+
let stack = [];
319+
let ans = [];
320+
while (root || stack.length) {
321+
while (root) {
322+
ans.push(root.val);
323+
stack.push(root);
324+
root = root.left;
325+
}
326+
root = stack.pop();
327+
root = root.right;
328+
}
329+
return ans;
330+
};
331+
```
332+
299333
### **C++**
300334

301335
```cpp

solution/0100-0199/0144.Binary Tree Preorder Traversal/README_EN.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,40 @@ class Solution {
265265
}
266266
```
267267

268+
### **TypeScript**
269+
270+
```ts
271+
/**
272+
* Definition for a binary tree node.
273+
* class TreeNode {
274+
* val: number
275+
* left: TreeNode | null
276+
* right: TreeNode | null
277+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
278+
* this.val = (val===undefined ? 0 : val)
279+
* this.left = (left===undefined ? null : left)
280+
* this.right = (right===undefined ? null : right)
281+
* }
282+
* }
283+
*/
284+
285+
function preorderTraversal(root: TreeNode | null): number[] {
286+
if (root == null) return [];
287+
let stack = [];
288+
let ans = [];
289+
while (root || stack.length) {
290+
while (root) {
291+
ans.push(root.val);
292+
stack.push(root);
293+
root = root.left;
294+
}
295+
root = stack.pop();
296+
root = root.right;
297+
}
298+
return ans;
299+
};
300+
```
301+
268302
### **C++**
269303

270304
```cpp
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 preorderTraversal(root: TreeNode | null): number[] {
16+
if (root == null) return [];
17+
let stack = [];
18+
let ans = [];
19+
while (root || stack.length) {
20+
while (root) {
21+
ans.push(root.val);
22+
stack.push(root);
23+
root = root.left;
24+
}
25+
root = stack.pop();
26+
root = root.right;
27+
}
28+
return ans;
29+
};

0 commit comments

Comments
 (0)