中序遍历示例

先打印左边的,再打印当前的,再打印右边的

  1. class Node {
  2. constructor(value) {
  3. this.value = value;
  4. this.left = null;
  5. this.right = null;
  6. }
  7. }
  8. let a = new Node("a");
  9. let b = new Node("b")
  10. let c = new Node("c")
  11. let d = new Node("d")
  12. let e = new Node("e")
  13. let f = new Node("f")
  14. let g = new Node("g")
  15. a.left = c
  16. a.right = b
  17. c.left = f
  18. c.right = g
  19. b.left = d
  20. b.right = e
  21. function fl(root) {
  22. if(root == undefined) return;
  23. fl(root.left)
  24. console.log(root.value)
  25. fl(root.right)
  26. }
  27. fl(a)