二叉树:每个节点最多有两个子节点
先序遍历:根左右
const bt = {val: 1,left:{val:2,left:{val: 4,left: null,right:null,},right: {val:5,left:null,right:null,}},right:{val:3,left:{val:6,left:null,right:null,},right:{val:7,left:null,right:null,}}}//先序遍历const preorder = (root) =>{if(!root) returnconsole.log(root.val)preorder(root.left)preorder(root.right)}
中序遍历:左根右
const inorder = (root) => {
if(!root) return
inorder(root.left)
console.log(root.val)
inorder(root.right)
}
后序遍历:左右中
const postorder = (root) =>{
if(!root) return
postorder(root.left)
postorder(root.right)
console.log(root.val)
}
