Skip to content

Commit

Permalink
Merge pull request #2595 from fahim041/0144-binary-tree-preorder-trav…
Browse files Browse the repository at this point in the history
…ersal.ts

create: 0144-binary-tree-preorder-traversal.ts
  • Loading branch information
tahsintunan committed Jun 23, 2023
2 parents 1f5f85a + 91ac80d commit d2a20ad
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions typescript/0144-binary-tree-preorder-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 preorderTraversal(root: TreeNode | null): number[] {
let res: number[] = [];

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

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

dfs(root);
return res;
}

0 comments on commit d2a20ad

Please sign in to comment.