HZFE / algorithms-solving

1 stars 0 forks source link

2022-10-17 #80

Open github-actions[bot] opened 2 years ago

gongpeione commented 2 years ago

144 Binary Tree Preorder Traversal

/*
 * @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

gongpeione commented 2 years ago

144 Binary Tree Preorder Traversal

/*
 * @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