Open github-actions[bot] opened 2 years ago
/*
* @lc app=leetcode id=144 lang=typescript
*
* [144] Binary Tree Preorder Traversal
*/
// @lc code=start
/**
* 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[] {
const ans = [];
const walk = (node) => {
node && ans.push(node.val);
node?.left && walk(node.left);
node?.right && walk(node.right);
}
walk(root);
return ans;
};
// @lc code=end
Nickname: Geeku From vscode-hzfe-algorithms
144 Binary Tree Preorder Traversal