站点工具

用户工具


二叉树后序遍历

题目

牛客网-后序遍历

前序、中序、后序

前序(中-左-右): A、B、D、E、C、F、G

中序(左-中-右):D、B、E、A、F、C、G

后序(左-右-中):D、E、B、F、G、C、A

后序遍历

function postorderTraversal( root ) {
    let arr = []
    function traversal(root, arr) {
        if(root === null) return 
        traversal(root.left, arr)
        traversal(root.right, arr)
        arr.push(root.val)
    }
    traversal(root, arr)
    return arr
}
若愚 · 2022/02/23 17:23 · 算法专刷_二叉树的后序遍历.txt