树 是一种经常用到的数据结构,用来模拟具有树状结构性质的数据集合

简单的树节点

  1. /**
  2. * 前置排序 中左右
  3. * 中置排序 左中右
  4. * 尾置排序 左右中 == (中右左的反序 和前置排序类型)
  5. * 都属于DFS深度优先搜索
  6. */
  7. class TreeNode {
  8. int val;
  9. TreeNode left;
  10. TreeNode right;
  11. TreeNode() {
  12. }
  13. TreeNode(int val) {
  14. this.val = val;
  15. }
  16. TreeNode(int val, TreeNode left, TreeNode right) {
  17. this.val = val;
  18. this.left = left;
  19. this.right = right;
  20. }
  21. }

顺序存储的二叉树

顺序存储的二叉树一般只考虑完全二叉树

一般规律规律

  1. 第n个元素的左子节点是:2*n+1
  2. 第n个元素的右子节点是:2*n+2
  3. 第n个元素的父节点是:(n-1)/2

    顺序存储二叉树遍历 - 前序遍历

    1. /**
    2. * @author LGC
    3. * @date 2022/4/4 11:40
    4. */
    5. public class ArrayBinaryThree {
    6. private int[] arrs;
    7. public ArrayBinaryThree(int[] arrs) {
    8. this.arrs = arrs;
    9. }
    10. /**
    11. * 顺序存储二叉树遍历 前序遍历
    12. */
    13. public void frontSort() {
    14. frontSort(0);
    15. }
    16. public void frontSort(int index) {
    17. if (arrs == null || arrs.length < 1) {
    18. return;
    19. }
    20. System.out.println(arrs[index]);
    21. // 左节点
    22. if ((2 * index + 1) < arrs.length) {
    23. frontSort(2 * index + 1);
    24. }
    25. // 有节点
    26. if ((2 * index + 2) < arrs.length) {
    27. frontSort(2 * index + 2);
    28. }
    29. }
    30. }

    DSF 深度优先搜索

    DSF 前置排序-利用队列 中左右

    1. /**
    2. * DSF 前置排序-利用队列 中左右
    3. * 利用队列的先进先出特性
    4. */
    5. public List<Integer> preOrder1(TreeNode root) {
    6. List<Integer> result = new ArrayList<>();
    7. Queue<TreeNode> queue = new LinkedList<>();
    8. if (root == null) {
    9. return result;
    10. }
    11. queue.offer(root);
    12. while (!queue.isEmpty()) {
    13. TreeNode poll = queue.poll();
    14. result.add(poll.val);
    15. if (poll.left != null) {
    16. queue.offer(poll.left);
    17. }
    18. if (poll.right != null) {
    19. queue.offer(poll.right);
    20. }
    21. }
    22. return result;
    23. }

    DSF 前置排序-利用栈 中左右

    1. /**
    2. * DSF 前置排序-利用栈 中左右
    3. * 利用栈的后入先出特性
    4. */
    5. public List<Integer> preOrder2(TreeNode root) {
    6. List<Integer> result = new ArrayList<>();
    7. Stack<TreeNode> stack = new Stack<>();
    8. if (root == null) {
    9. return result;
    10. }
    11. stack.push(root);
    12. while (!stack.isEmpty()) {
    13. TreeNode peek = stack.pop();
    14. if (peek != null) {
    15. // 中
    16. result.add(peek.val);
    17. // 右
    18. if (peek.right != null) {
    19. stack.push(peek.right);
    20. }
    21. // 左
    22. if (peek.left != null) {
    23. stack.push(peek.left);
    24. }
    25. }
    26. }
    27. return result;
    28. }

    DSF 中置排序-利用栈 左中右

    1. /**
    2. * DSF 中置排序-利用栈 左中右
    3. * 利用栈的后入先出特性
    4. */
    5. public List<Integer> middleOrder(TreeNode root) {
    6. List<Integer> result = new ArrayList<>();
    7. if (root == null) {
    8. return result;
    9. }
    10. Stack<TreeNode> stack = new Stack<>();
    11. while (root != null || !stack.isEmpty()) {
    12. // 先把所有左边放入栈中
    13. while (root != null) {
    14. stack.push(root);
    15. root = root.left;
    16. }
    17. // 先输出最左边数据 再次循环
    18. if (!stack.isEmpty()) {
    19. root = stack.pop();
    20. result.add(root.val);
    21. root = root.right;
    22. }
    23. }
    24. return result;
    25. }

    DSF 尾置排序-利用栈 左右中

    1. /**
    2. * DSF 尾置排序-利用栈 左右中
    3. * 利用栈的后入先出特性
    4. * 尾置排序 左右中 == (中右左的反序 和前置排序类似)
    5. */
    6. public List<Integer> endOrder(TreeNode root) {
    7. List<Integer> result = new ArrayList<>();
    8. if (root == null) {
    9. return result;
    10. }
    11. Stack<TreeNode> stack = new Stack<>();
    12. stack.add(root);
    13. while (!stack.isEmpty()) {
    14. TreeNode pop = stack.pop();
    15. if (pop != null) {
    16. result.add(pop.val);
    17. if (pop.left != null) {
    18. stack.push(pop.left);
    19. }
    20. if (pop.right != null) {
    21. stack.push(pop.right);
    22. }
    23. }
    24. }
    25. Collections.reverse(result);
    26. return result;
    27. }

    BSF 广度优先搜索

    BSF 利用队列 二叉树的层序遍历

    1. /**
    2. * BSF 利用队列 输出同一行数据
    3. */
    4. public List<List<Integer>> levelOrder(TreeNode root) {
    5. List<List<Integer>> result = new ArrayList<>();
    6. Queue<TreeNode> queue = new LinkedList<>();
    7. if (root == null) {
    8. return result;
    9. }
    10. queue.offer(root);
    11. while (!queue.isEmpty()) {
    12. List<Integer> levelList = new ArrayList<>();
    13. int size = queue.size();
    14. for (int i = 0; i < size; i++) {
    15. TreeNode peek = queue.poll();
    16. if (peek != null) {
    17. levelList.add(peek.val);
    18. if (peek.left != null) {
    19. queue.offer(peek.left);
    20. }
    21. if (peek.right != null) {
    22. queue.offer(peek.right);
    23. }
    24. }
    25. }
    26. result.add(levelList);
    27. }
    28. return result;
    29. }

    BSF 利用递归 二叉树的层序遍历

    ```java /**

    • BSF 递归输出同一行数据 */ public List> levelOrder2(TreeNode root) { List> result = new ArrayList<>(); order(result, root, 0); return result; }

private void order(List> list, TreeNode node, int level) { if (node == null) { return; } if (list.size() <= level) { list.add(new ArrayList<>()); } list.get(level).add(node.val); order(list, node.left, level + 1); order(list, node.right, level + 1); }

  1. <a name="StQXJ"></a>
  2. # 利用递归
  3. <a name="bE5wK"></a>
  4. ## 二叉树最大深度
  5. ```java
  6. /**
  7. * 二叉树最大深度
  8. */
  9. public int maxDepth(TreeNode root) {
  10. if (root == null) {
  11. return 0;
  12. }
  13. int leftDepth = maxDepth(root.left);
  14. int rightDepth = maxDepth(root.right);
  15. return Math.max(leftDepth, rightDepth) + 1;
  16. }

是否是对称二叉树

  1. /**
  2. * 是否是对称二叉树
  3. * 两个节点同时为空true,两个节点其中一个为空false
  4. * 两个节点相等 且 左节点的左子树等于有节点的右子树 且 左节点的右子树等于右节点的左子树,为镜像返回true。
  5. */
  6. public boolean isSymmetric(TreeNode root) {
  7. return isMirror(root, root);
  8. }
  9. public boolean isMirror(TreeNode t1, TreeNode t2) {
  10. if (t1 == null && t2 == null) {
  11. return true;
  12. }
  13. if (t1 == null || t2 == null) {
  14. return false;
  15. }
  16. return (t1.val == t2.val)
  17. && isMirror(t1.left, t2.right)
  18. && isMirror(t1.right, t2.left);
  19. }

路径总和

  1. /**
  2. * 路径总和
  3. * 最后一个值一定等于剩余值
  4. */
  5. public boolean hasPathSum(TreeNode root, int targetSum) {
  6. if (root == null) {
  7. return false;
  8. }
  9. if (root.left == null && root.right == null) {
  10. return root.val == targetSum;
  11. }
  12. return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
  13. }