1、前序遍历

https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
递归法

  1. var inorderTraversal = function(root) {
  2. const res = [];
  3. const inorder = (root) => {
  4. if (!root) {
  5. return;
  6. }
  7. res.push(root.val);
  8. inorder(root.left);
  9. inorder(root.right);
  10. }
  11. inorder(root);
  12. return res;
  13. };

迭代法 stack

  1. var preorderTraversal = function(root) {
  2. let stack = [root]
  3. let res = []
  4. while (stack.length) {
  5. let node = stack.pop()
  6. //注意,node为空时,不忘stack中push子节点
  7. if (node) {
  8. res.push(node.val)
  9. stack.push(node.right)
  10. stack.push(node.left)
  11. }
  12. }
  13. return res
  14. };

2、中序遍历

递归法

  1. var inorderTraversal = function(root) {
  2. const res = [];
  3. const inorder = (root) => {
  4. if (!root) {
  5. return;
  6. }
  7. inorder(root.left);
  8. res.push(root.val);
  9. inorder(root.right);
  10. }
  11. inorder(root);
  12. return res;
  13. };

迭代法
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/er-cha-shu-de-zhong-xu-bian-li-by-leetcode-solutio/

  1. var inorderTraversal = function(root) {
  2. let stack = []
  3. let res = []
  4. while (root || stack.length) {
  5. //重要,将左子树入栈
  6. while (root) {
  7. stack.push(root)
  8. root = root.left
  9. }
  10. //想象根节点的右侧
  11. root = stack.pop()
  12. res.push(root.val)
  13. root = root.right
  14. }
  15. return res
  16. };

3、后序遍历

递归法

  1. var inorderTraversal = function(root) {
  2. const res = [];
  3. const inorder = (root) => {
  4. if (!root) {
  5. return;
  6. }
  7. inorder(root.left);
  8. inorder(root.right);
  9. res.push(root.val);
  10. }
  11. inorder(root);
  12. return res;
  13. };

迭代法

  1. var postorderTraversal = function(root) {
  2. let stack = [root]
  3. let res = []
  4. while (stack.length) {
  5. let node = stack.pop()
  6. if (node) {
  7. stack.push(node.left, node.right)
  8. res.unshift(node.val)
  9. }
  10. }
  11. return res
  12. };

4、层序遍历

递归法
迭代法
https://leetcode-cn.com/problems/binary-tree-level-order-traversal/

  1. var inorder = function(root) {
  2. if (!root) return []
  3. const res = []
  4. const que = [root]
  5. while (que.length) {
  6. let len = que.length
  7. let arr = []
  8. //重点
  9. while (len--) {
  10. let node = que.shift()
  11. arr.push(node.val)
  12. if (node.right) que.push(node.right)
  13. if (node.left) que.push(node.left)
  14. }
  15. res.push(arr)
  16. }
  17. return res
  18. }

5、判断两个二叉树是否相同

递归
image.png

6、二叉树的最小深度

7、将有序数组转换为二叉搜索树

解答
image.png

8、二叉树的镜像

https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/

  1. var mirrorTree = function(root) {
  2. if (root == null) {
  3. return null;
  4. }
  5. const left = mirrorTree(root.left);
  6. const right = mirrorTree(root.right);
  7. root.left = right;
  8. root.right = left;
  9. return root;
  10. };

9、二叉树的最大深度

  1. var maxDepth = function(root) {
  2. if (!root) return 0
  3. return Math.max(maxDepth(root.left), maxDepth(root.right)) +1
  4. };