File tree 3 files changed +122
-0
lines changed
solution/0100-0199/0145.Binary Tree Postorder Traversal
3 files changed +122
-0
lines changed Original file line number Diff line number Diff line change @@ -259,6 +259,49 @@ class Solution {
259
259
}
260
260
```
261
261
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
+
262
305
### ** C++**
263
306
264
307
``` cpp
Original file line number Diff line number Diff line change @@ -269,6 +269,48 @@ class Solution {
269
269
}
270
270
```
271
271
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
+
272
314
### ** C++**
273
315
274
316
``` 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 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
+ } ;
You can’t perform that action at this time.
0 commit comments