Skip to content
字数
120 字
阅读时间
1 分钟
ts
/**

 * 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 result: number[] = [];

    const stack: TreeNode[] = [];

    let current: TreeNode|null = root;

    while (current !== null || stack.length > 0) {

  
  

        while (current !== null) {

            stack.push(current);

            current =current.left;

  

        }

        current = stack.pop()!;

        result.push(current.val);

  

        current = current.right

  

    }

    return result

};

贡献者

The avatar of contributor named as sunchengzhi sunchengzhi

文件历史

撰写