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 003ab93

Browse files
feat: add typescript solution to lc problem: No.0145.Binary Tree Postorder Traversal
1 parent a2b4cb0 commit 003ab93

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

‎solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,49 @@ class Solution {
259259
}
260260
```
261261

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

264307
```cpp

‎solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,48 @@ class Solution {
269269
}
270270
```
271271

272+
### **TypeScript**
273+
274+
```ts
275+
/**
276+
* Definition for a binary tree node.
277+
* class TreeNode {
278+
* val: number
279+
* left: TreeNode | null
280+
* right: TreeNode | null
281+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
282+
* this.val = (val===undefined ? 0 : val)
283+
* this.left = (left===undefined ? null : left)
284+
* this.right = (right===undefined ? null : right)
285+
* }
286+
* }
287+
*/
288+
289+
function postorderTraversal(root: TreeNode | null): number[] {
290+
if (root == null) return [];
291+
let stack = [];
292+
let ans = [];
293+
let prev = null;
294+
while (root || stack.length) {
295+
while (root) {
296+
stack.push(root);
297+
root = root.left;
298+
}
299+
root = stack.pop();
300+
if (!root.right || root.right == prev) {
301+
ans.push(root.val);
302+
prev = root;
303+
root = null;
304+
} else {
305+
stack.push(root);
306+
root = root.right;
307+
}
308+
309+
}
310+
return ans;
311+
};
312+
```
313+
272314
### **C++**
273315

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

0 commit comments

Comments
(0)

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