Skip to content

Commit

Permalink
create: 0145-binary-tree-postorder-traversal.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
fahim041 committed Jun 19, 2023
1 parent 914ca1f commit 9bb1b6d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions typescript/0145-binary-tree-postorder-traversal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function postorderTraversal(root: TreeNode | null): number[] {
let res: number[] = [];

function dfs(root) {
if (!root) {
return;
}

dfs(root.left);
dfs(root.right);
res.push(root.val);
}

dfs(root);
return res;
}

0 comments on commit 9bb1b6d

Please sign in to comment.