基础算法之二叉树遍历

    科技2025-11-03  9

    基础算法之二叉树遍历

    二叉树的遍历分为前序、中序和后序遍历 ---------------有不足之出还需要各位在评论区批评指正 正文: ····前序遍历:是指首先从根节点开始,再依次寻找左节点、右节点。 打印顺序为: r , a , c , d ,b , f 代码为:

    function before(root){ if(root === null) return console.log(root.value) before(root.left) before(root.right) // r , a , c , d ,b , f } ····中序遍历:是指首先从左节点开始遍历,再到根节点,然后到右节点。 打印顺序为:c , a , d , r , d , f function mid(root){ if(root === null) return mid(root.left) console.log(root.value) mid(root.right) //c , a , d , r , d , f } ····后序遍历:是指首先从左节点开始遍历,依次是右节点、根节点。 打印顺序为: c , d , a , f , b , r function after(root){ if(root === null) return after(root.left) after(root.right) console.log(root.value) // c , d , a , f , b , r }
    Processed: 0.015, SQL: 8