94. 二叉树的中序遍历

题目

  • 给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。

递归

var inorderTraversal = function(root) {
    const res=[] //结果数组
    const inorder = (root)=>{//递归函数
        if(root === null) return//遇到空(底)返回
        inorder(root.left)//先访问左子树
        res.push(root.val)//访问当前节点
        inorder(root.right)//最后访问右子树
    }
    inorder(root)//传入整个树
    return res//返回结果列表
posted @ 2025-01-11 09:52  Frommoon  阅读(6)  评论(0)    收藏  举报