二叉树:每个节点最多有两个子节点
    先序遍历:根左右
    image.png

    1. const bt = {
    2. val: 1,
    3. left:{
    4. val:2,
    5. left:{
    6. val: 4,
    7. left: null,
    8. right:null,
    9. },
    10. right: {
    11. val:5,
    12. left:null,
    13. right:null,
    14. }
    15. },
    16. right:{
    17. val:3,
    18. left:{
    19. val:6,
    20. left:null,
    21. right:null,
    22. },
    23. right:{
    24. val:7,
    25. left:null,
    26. right:null,
    27. }
    28. }
    29. }
    30. //先序遍历
    31. const preorder = (root) =>{
    32. if(!root) return
    33. console.log(root.val)
    34. preorder(root.left)
    35. preorder(root.right)
    36. }

    中序遍历:左根右
    image.png

    const inorder = (root) => {
        if(!root) return
        inorder(root.left)
        console.log(root.val)
        inorder(root.right)
    }
    

    后序遍历:左右中
    image.png

    const postorder = (root) =>{
        if(!root) return
        postorder(root.left)
        postorder(root.right)
        console.log(root.val)
    }