概念

链式存储

1. 先序遍历

2.1 递归解法

解题思路
代码

  1. preorder_recur(node) {
  2. if (!node) {
  3. node = this.root
  4. }
  5. console.log(node.data)
  6. this.preorder_recur(node.left)
  7. this.preorder_recur(node.right)
  8. }

1.2 非递归解法

解题思路
代码

  1. preorder(node) {
  2. if (!node) {
  3. node = this.root
  4. }
  5. let stack = []
  6. let res = []
  7. stack.push(node)
  8. if (node === null) {
  9. return []
  10. }
  11. while (stack.length > 0) {
  12. let node = stack.pop()
  13. res.push(node.val)
  14. if (node.right) {
  15. stack.push(node.right)
  16. }
  17. if (node.left) {
  18. stack.push(node.left)
  19. }
  20. }
  21. return res
  22. }

2. 中序遍历

2.1 递归解法

  1. if (!node) {
  2. node = this.root
  3. }
  4. this.preorder_recur(node.left)
  5. console.log(node.data)
  6. this.preorder_recur(node.right)

2.2 非递归解法

  1. inorder(node) {
  2. if (!node) {
  3. node = this.root
  4. }
  5. let stack = []
  6. let res = []
  7. if (node === null) {
  8. return []
  9. }
  10. while (node !== null || stack.length > 0) {
  11. while (node !== null) {
  12. stack.push(node)
  13. node = node.left
  14. }
  15. node = stack.pop()
  16. res.push(node.data)
  17. node = node.right
  18. }
  19. }

3. 后序遍历

3.1 递归解法

  1. postorder_recur(node) {
  2. if (!node) {
  3. node = this.root
  4. }
  5. this.preorder_recur(node.left)
  6. this.preorder_recur(node.right)
  7. console.log(node.data)
  8. }

3.2 非递归解法

  1. postorder(node) {
  2. if (!node) {
  3. node = this.root
  4. }
  5. let stack = []
  6. let res = []
  7. if (node === null) {
  8. return []
  9. }
  10. stack.push(node)
  11. while (stack.length > 0) {
  12. let node = stack.pop()
  13. res.unshift(node.val)
  14. if (node.left) {
  15. stack.push(node.left)
  16. }
  17. if (node.right) {
  18. stack.push(node.right)
  19. }
  20. }
  21. }

4. 层序遍历

  1. levelOrder(root) {
  2. let nodequeue = []
  3. let res = []
  4. if (root === null) {
  5. return []
  6. }
  7. nodequeue.push(root)
  8. let level = 0
  9. while (nodequeue.length > 0) {
  10. res[level] = []
  11. let len = nodequeue.length
  12. for (let i = 0; i < len; i++) {
  13. let node = nodequeue.shift()
  14. res[level].push(node.val)
  15. if (node.left) {
  16. nodequeue.push(node.left)
  17. }
  18. if (node.right) {
  19. nodequeue.push(node.right)
  20. }
  21. }
  22. level++
  23. }
  24. }

5. 路径查找

  1. binaryTreePaths(root) {
  2. let nodestack = []
  3. let res = []
  4. let pathstack = []
  5. if (root === null) {
  6. return res
  7. }
  8. nodestack.push(root)
  9. pathstack.push(root.val.toString())
  10. while (nodestack.length > 0) {
  11. let node = nodestack.pop()
  12. let path = pathstack.pop()
  13. if (node.left === null && node.right === null) {
  14. res.unshift(path)
  15. }
  16. if (node.left) {
  17. nodestack.push(node.left)
  18. pathstack.push(path + '->' + node.left.val)
  19. }
  20. if (node.right) {
  21. nodestack.push(node.right)
  22. pathstack.push(path + '->' + node.right.val)
  23. }
  24. }
  25. return res
  26. }

线性存储