题解 | #二叉树的后序遍历#

二叉树的后序遍历

https://www.nowcoder.com/practice/1291064f4d5d4bdeaefbf0dd47d78541

export function postorderTraversal(root: TreeNode): number[] {
    // write code here
    const res = []
    function traversal(root){
        if(root){
            // push在这里是前序遍历
            traversal(root.left);
            // push在这里是中序遍历
            traversal(root.right)
            // push放在这里是后续遍历
            res.push(root.val)
        }
    }
    traversal(root)
    return res
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务