LC-二叉树的中序遍历(JavaScript实现)

阅读 45

2022-02-05

/*
 * @lc app=leetcode.cn id=94 lang=javascript
 *
 * [94] 二叉树的中序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function(root) {
    let ans = [];
    const inorder = root => {
        if(root==null) return;
        inorder(root.left)
        ans.push(root.val)
        inorder(root.right)
    }
    inorder(root);
    return ans;
};
// @lc code=end


精彩评论(0)

0 0 举报