Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-09-28 #374

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-09-28

Dapeus commented 2 years ago

image

gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=94 lang=typescript
 *
 * [94] Binary Tree Inorder 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 inorderTraversal(root: TreeNode | null): number[] {
    const inorder = [];

    const walk = (node: TreeNode | null) => {
        if (!node) return;
        if (node.left) {
            walk(node.left);
        }
        inorder.push(node.val);
        if (node.right) {
            walk(node.right);
        }
    }

    walk(root);

    return inorder;
};
// @lc code=end

微信id: 弘树 来自 vscode 插件