• 二叉树相关算法


    1、二叉树基本操作

    二叉树的定义就不在这里多说了,下面这个图就是一个简单的二叉树:

    二叉树的三种遍历方式:

    前序遍历:头左右,也就是先头后左再右:1245367

    1. public static void prePrint(BinaryTreeNode root) {
    2. if (root != null) {
    3. System.err.print(root.val);
    4. prePrint(root.left);
    5. prePrint(root.right);
    6. }
    7. }
    8. //非递归方式
    9. public static void prePrintStack(BinaryTreeNode root) {
    10. if (root != null) {
    11. Stack<BinaryTreeNode> queue = new Stack<>();
    12. queue.add(root);
    13. while (!queue.isEmpty()) {
    14. BinaryTreeNode poll = queue.pop();
    15. System.err.print(poll.val);
    16. if (poll.right != null) {
    17. queue.add(poll.right);
    18. }
    19. if (poll.left != null) {
    20. queue.add(poll.left);
    21. }
    22. }
    23. }
    24. }

    中序遍历:左头右,也就是先左后头再右:4251637

    1. public static void midPrint(BinaryTreeNode root) {
    2. if (root != null) {
    3. midPrint(root.left);
    4. System.err.print(root.val);
    5. midPrint(root.right);
    6. }
    7. }
    8. //非递归方式
    9. public static void midPrintStack(BinaryTreeNode root) {
    10. if (root != null) {
    11. Stack<BinaryTreeNode> queue = new Stack<>();
    12. BinaryTreeNode current = root;
    13. while (current != null || !queue.isEmpty()) {
    14. while (current != null) {
    15. queue.push(current);
    16. current = current.left;
    17. }
    18. current = queue.pop();
    19. System.err.print(current.val);
    20. current = current.right;
    21. }
    22. }
    23. }

    后序遍历:左头右,也就是先左后右再头:4526731

    1. public static void posPrint(BinaryTreeNode root) {
    2. if (root != null) {
    3. posPrint(root.left);
    4. posPrint(root.right);
    5. System.err.print(root.val);
    6. }
    7. }
    8. //非递归方式
    9. public void posPrintStack(BinaryTreeNode root) {
    10. Stack<BinaryTreeNode> stack = new Stack<>();
    11. stack.push(root);
    12. BinaryTreeNode prev = null;
    13. while (!stack.isEmpty()) {
    14. BinaryTreeNode current = stack.peek();
    15. if (prev == null || prev.left == current || prev.right == current) {
    16. if (current.left != null) {
    17. stack.push(current.left);
    18. } else if (current.right != null) {
    19. stack.push(current.right);
    20. }
    21. } else if (current.left == prev) {
    22. if (current.right != null) {
    23. stack.push(current.right);
    24. }
    25. } else {
    26. System.err.print(current.val);
    27. stack.pop();
    28. }
    29. prev = current;
    30. }
    31. }

    测试代码:

    1. class BinaryTreeNode {
    2. int val;
    3. BinaryTreeNode left;
    4. BinaryTreeNode right;
    5. public BinaryTreeNode(int val) {
    6. this.val = val;
    7. }
    8. public BinaryTreeNode(int val, BinaryTreeNode left, BinaryTreeNode right) {
    9. this.val = val;
    10. this.left = left;
    11. this.right = right;
    12. }
    13. }
    1. public static void main(String[] args) {
    2. BinaryTreeNode one = new BinaryTreeNode(1,
    3. new BinaryTreeNode(2, new BinaryTreeNode(4, null, null), new BinaryTreeNode(5, null, null)),
    4. new BinaryTreeNode(3, new BinaryTreeNode(6, null, null), new BinaryTreeNode(7, null, null)));
    5. prePrint(one);
    6. System.err.println();
    7. midPrint(one);
    8. System.err.println();
    9. posPrint(one);
    10. }

    那么我们可以看出来,不论是哪种遍历方式,其在处理左右子节点的时候,逻辑都是一样的,都是要递归处理,不同的只是头结点的输出时机,那么可以优化成下面的代码:

    1. public static void print(BinaryTreeNode root, int type) {
    2. switch (type) {
    3. case 1:
    4. if (root != null) {
    5. System.err.print(root.val);
    6. print(root.left, 1);
    7. print(root.right, 1);
    8. }
    9. break;
    10. case 2:
    11. if (root != null) {
    12. print(root.left, 2);
    13. System.err.print(root.val);
    14. print(root.right, 2);
    15. }
    16. break;
    17. case 3:
    18. if (root != null) {
    19. print(root.left, 3);
    20. print(root.right, 3);
    21. System.err.print(root.val);
    22. }
    23. break;
    24. }
    25. }

    ps:由此可见,递归换做stack通过非递归来实现的时候,如果对顺序由要求,代码要复杂难懂的多

    2、两棵树是否结构一样

    如下面的图中,只有左上角和右上角两棵树的结构是一样的,才算符合条件:

    Java实现判断两棵树是否一样:

    1. public static boolean isSameTree(BinaryTreeNode node1, BinaryTreeNode node2) {
    2. if (node1 == null ^ node2 == null) {
    3. return false;
    4. }
    5. if (node1 == null && node2 == null) {
    6. return true;
    7. }
    8. return node1.val == node2.val && isSameTree(node1.left, node2.left) && isSameTree(node1.right, node2.right);
    9. }
    10. @Test
    11. public void testSame() {
    12. BinaryTreeNode one = new BinaryTreeNode(1,
    13. new BinaryTreeNode(2, new BinaryTreeNode(4, null, null), new BinaryTreeNode(5, null, null)),
    14. new BinaryTreeNode(3, new BinaryTreeNode(6, null, null), new BinaryTreeNode(7, null, null)));
    15. BinaryTreeNode two = new BinaryTreeNode(1,
    16. new BinaryTreeNode(2, new BinaryTreeNode(4, null, null), new BinaryTreeNode(5, null, null)),
    17. new BinaryTreeNode(3, new BinaryTreeNode(6, null, null), new BinaryTreeNode(7, null, null)));
    18. System.err.println(isSameTree(one, two));
    19. }

    3、镜面树

    镜面树有两种情况:

    1. 两棵树互为镜面:按照红线对折,可以重叠 
    2. 单棵树两边互为镜面:代码实现:
      1. public static boolean isMirror(BinaryTreeNode node1, BinaryTreeNode node2) {
      2. if (node1 == null ^ node2 == null) {
      3. return false;
      4. }
      5. if (node1 == null && node2 == null) {
      6. return true;
      7. }
      8. return node1.val == node2.val && isMirror(node1.left, node2.right) && isMirror(node1.right, node2.left);
      9. }
      10. @Test
      11. public void testMirror() {
      12. BinaryTreeNode one = new BinaryTreeNode(1,
      13. new BinaryTreeNode(2, new BinaryTreeNode(4, null, null), new BinaryTreeNode(5, null, null)),
      14. new BinaryTreeNode(2, new BinaryTreeNode(5, null, null), new BinaryTreeNode(4, null, null)));
      15. BinaryTreeNode two = new BinaryTreeNode(1,
      16. new BinaryTreeNode(2, new BinaryTreeNode(4, null, null), new BinaryTreeNode(5, null, null)),
      17. new BinaryTreeNode(2, new BinaryTreeNode(5, null, null), new BinaryTreeNode(4, null, null)));
      18. System.err.println(isMirror(one, two));
      19. }

     4、树的最大深度

    二叉树的最大深度其实就是左子树和右子树的最大深度加一,代码实现如下:

    1. public static int maxDepth(BinaryTreeNode root) {
    2. if (root == null) {
    3. return 0;
    4. }
    5. return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    6. }
    7. @Test
    8. public void testMaxDepth() {
    9. BinaryTreeNode two = new BinaryTreeNode(1,
    10. new BinaryTreeNode(2, new BinaryTreeNode(4, null, null), new BinaryTreeNode(5, null, null)),
    11. new BinaryTreeNode(2, new BinaryTreeNode(5, null, null), new BinaryTreeNode(4, null, null)));
    12. System.err.println(maxDepth(two));
    13. }

    5、还原二叉树

    给定一个二叉树的前序遍历数组和中序遍历数组,两个数组中都没有重复值,根据这两个数组还原二叉树,返回头结点

    解题思路:仍然是采用递归的方式,几个重要的解题点:

    1、前序遍历的第一个元素一定是树的头结点,比如下面这个最基本的二叉树,前序遍历是1,2,3,中序遍历是2,1,3,所以树的头结点一定是1

    2、找出中序遍历数组中头结点所在的位置,假设下标为A,那么在前序遍历数组中,从头结点所在下标加1到A(包括两端),以及在中序序遍历数组中从0到A减1(包括两端)的位置都是左子树的节点

    比如下面这棵树的头结点是1,在中序遍历数组中的下标是1,那么2就是左子树,再比如文章最前面的第一棵二叉树,前序遍历1245367和中序遍历4251637,根据第二点我们可以得出前序遍历中的245和中序遍历中的425一定是左子树,右子树的逻辑也类似

     代码实现:

    1. public static BinaryTreeNode buildTree(int[] preorder, int[] inorder) {
    2. // 构建中序遍历数组中元素与索引的映射关系
    3. Map<Integer, Integer> inorderMap = new HashMap<>();
    4. for (int i = 0; i < inorder.length; i++) {
    5. inorderMap.put(inorder[i], i);
    6. }
    7. return buildTreeHelper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, inorderMap);
    8. }
    9. private static BinaryTreeNode buildTreeHelper(int[] preorder, int preStart, int preEnd,
    10. int[] inorder, int inStart, int inEnd, Map<Integer, Integer> inorderMap) {
    11. if (preStart > preEnd || inStart > inEnd) {
    12. return null;
    13. }
    14. int rootVal = preorder[preStart];
    15. BinaryTreeNode root = new BinaryTreeNode(rootVal);
    16. if (preStart == preEnd) {//相等的时候说明要么是根节点,要么是到了最后一个节点
    17. return root;
    18. }
    19. int rootIndex = inorderMap.get(rootVal);
    20. int leftSize = rootIndex - inStart;
    21. root.left = buildTreeHelper(preorder, preStart + 1, preStart + leftSize,
    22. inorder, inStart, rootIndex - 1, inorderMap);
    23. root.right = buildTreeHelper(preorder, preStart + leftSize + 1, preEnd,
    24. inorder, rootIndex + 1, inEnd, inorderMap);
    25. return root;
    26. }
    27. @Test
    28. public void testBuildTree() {
    29. int[] preorder = {1, 2, 4, 5, 3, 6, 7};
    30. int[] inorder = {4, 2, 5, 1, 6, 3, 7};
    31. // int[] preorder = {1, 2, 3};
    32. // int[] inorder = {2, 1, 3};
    33. BinaryTreeNode root = buildTree(preorder, inorder);
    34. print(root, 1);
    35. System.err.println();
    36. print(root, 2);
    37. System.err.println();
    38. print(root, 3);
    39. }

    6、水平收集二叉树

    意思是按一层一层的输出二叉树的数据,比如下面的棵树:其输出结果是[[3, 4, 4, 3], [2, ], [1]]

     解题思路:

    1、用一个队列(栈也可以),存储每一层的节点,那么队列的size就是当前层的节点个数,比如上图的树中,先把1节点放进队列,那么队列的size就是1,然后从队列中把1节点取出来,这个时候队列的size就是0,同时如果取出来的节点有左右子节点,就把左右子节点都放到队列里面去,进行下一轮队列取数

    2、第一步是按顺序取出来了每一层的节点,第二部就是把取出来的多个数组倒序输出

    代码如下:

    1. public static List<List<Integer>> transverseColectTree(BinaryTreeNode root) {
    2. List<List<Integer>> result = new LinkedList<>();
    3. if (null != root) {
    4. Queue<BinaryTreeNode> queue = new LinkedList();
    5. queue.add(root);
    6. while (!queue.isEmpty()) {
    7. List<Integer> tmpList = new LinkedList<>();
    8. int size = queue.size();//队列的size就是每一层的节点个数
    9. for (int i = 0; i < size; i++) {//for循环结束之后,tmpList存储的就是当前层的节点
    10. BinaryTreeNode poll = queue.poll();
    11. tmpList.add(poll.val);
    12. if (poll.left != null) {
    13. queue.add(poll.left);
    14. }
    15. if (poll.right != null) {
    16. queue.add(poll.right);
    17. }
    18. }
    19. result.add(0, tmpList);
    20. }
    21. }
    22. return result;
    23. }
    24. @Test
    25. public void testCransverseColectTree() {
    26. BinaryTreeNode one = new BinaryTreeNode(1,
    27. new BinaryTreeNode(2, new BinaryTreeNode(4, null, null), new BinaryTreeNode(5, null, null)),
    28. new BinaryTreeNode(3, new BinaryTreeNode(6, null, null), new BinaryTreeNode(7, null, null)));
    29. System.err.println(transverseColectTree(one));
    30. }

    7、判断一棵树是否平衡二叉树

    平衡二叉树的概念:对任意一个节点来说,它的左子树的高度和右子树的高度差不超过1

    解题思路:我们前面已经右现成的可以求一个节点的最大高度maxDepth函数可以直接用,那么只需要判断当前节点本身是否一个平衡树并且左右子树的高度差不超过1即可

    直接上代码:

    1. //获取root节点左右子树的最大高度
    2. public static int maxDepth(BinaryTreeNode root) {
    3. if (root == null) {
    4. return 0;
    5. }
    6. return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    7. }
    8. //判断root节点是否平衡树
    9. public static boolean isBalancedTree(BinaryTreeNode root) {
    10. if (root == null) {
    11. return true;
    12. }
    13. return isBalancedTree(root.left) && isBalancedTree(root.right) && Math.abs(maxDepth(root.left) - maxDepth(root.right)) < 1;
    14. }
    15. @Test
    16. public void testBalanced() {
    17. BinaryTreeNode one = new BinaryTreeNode(1,
    18. new BinaryTreeNode(2, new BinaryTreeNode(4, null, null), new BinaryTreeNode(5, null, null)),
    19. new BinaryTreeNode(3, new BinaryTreeNode(6, null, null), new BinaryTreeNode(7, null, null)));
    20. System.err.println(isBalancedTree(one));
    21. }

    8、判断一棵树是否搜索二叉树

    搜索二叉树的特点:

    1. 有序性:对于搜索二叉树中的任意节点,其左子树中的所有节点的值都小于该节点的值,而右子树中的所有节点的值都大于该节点的值。这意味着搜索二叉树中的节点是按照一定的顺序排列的。

    2. 唯一性:搜索二叉树中不允许有重复的节点。每个节点的值都是唯一的

    解题思路:有两种思路,一种是使用中序遍历的方式,因为二叉树的中序遍历是左头右的顺序,所以按照中序遍历出来的数组如果符合严格递增的顺序,那么这颗树就是搜索二叉树,另一种就是根据搜索二叉树的特点来通过递归实现,下面仅展示递归的写法:

    1. private boolean isValidBST(BinaryTreeNode node, Integer min, Integer max) {
    2. if (node == null) {
    3. return true;
    4. }
    5. //对于任意节点,左子树的最大值必须小于该节点的值且右子树的最小值必须大于该节点的值
    6. if ((min != null && node.val <= min) || (max != null && node.val >= max)) {
    7. return false;
    8. }
    9. return isValidBST(node.left, min, node.val) && isValidBST(node.right, node.val, max);
    10. }
    11. @Test
    12. public void testBst() {
    13. BinaryTreeNode one = new BinaryTreeNode(7,
    14. new BinaryTreeNode(5, new BinaryTreeNode(4, null, null), new BinaryTreeNode(6, null, null)),
    15. new BinaryTreeNode(8, null, new BinaryTreeNode(9, null, null)));
    16. System.err.println(isValidBST(one,null,null));
    17. }

    9、判断一棵树是否平衡搜索二叉树

    平衡搜索二叉树其实就是前面的第七和第八两个综合起来了,代码就不在这里写了

    10、路径和

    给定一颗二叉树和一个目标数,从树的根节点到叶子结点为一条完整的路径,如果有任何一条路径上的数字之和等于目标数,就返回true

    1. public boolean hasPathSum(BinaryTreeNode root, int sum) {
    2. if (null == root) {
    3. return false;
    4. }
    5. if (root.left == null && root.right == null) {
    6. return sum == root.val;
    7. }
    8. return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    9. }
    10. @Test
    11. public void testSumPath() {
    12. BinaryTreeNode one = new BinaryTreeNode(7,
    13. new BinaryTreeNode(5, new BinaryTreeNode(4, null, null), new BinaryTreeNode(6, null, null)),
    14. new BinaryTreeNode(8, new BinaryTreeNode(7, null, null), new BinaryTreeNode(9, null, null)));
    15. System.err.println(hasPathSum(one, 18));
    16. }

    11、路径和之二

    给定一颗二叉树和一个目标数,从树的根节点到叶子结点为一条完整的路径,找出路径上的数字之和等于目标数的所有路径

    1. public List<List<Integer>> pathSum(BinaryTreeNode root, int sum) {
    2. List<List<Integer>> result = new LinkedList<>();
    3. if (null == root) {
    4. return result;
    5. }
    6. pathSum(root, sum, new LinkedList<Integer>(), result);
    7. return result;
    8. }
    9. public void pathSum(BinaryTreeNode root, int targetSum, List<Integer> curPath, List<List<Integer>> result) {
    10. curPath.add(root.val);//先把当前节点加到路劲中
    11. if (root.left == null && root.right == null) {//当前节点是叶子节点
    12. if (root.val == targetSum) {
    13. result.add(new LinkedList<>(curPath));
    14. }
    15. } else {
    16. pathSum(root.left, targetSum - root.val, curPath, result);
    17. pathSum(root.right, targetSum - root.val, curPath, result);
    18. }
    19. curPath.remove(curPath.size() - 1);//不论当前节点是否满足,都需要删除还原现场,因为要返回上一级节点,继续算
    20. }

  • 相关阅读:
    从几个关键节点来理解nodejs
    SQL优化之MySQL执行计划(Explain)及索引失效详解
    linux☞container_of
    LeetCode每日一题:1222. 可以攻击国王的皇后(2023.9.14 C++)
    Sublime Text Mac/Win中文版:代码编辑器的卓越典范
    机器学习笔记之受限玻尔兹曼机(四)推断任务——边缘概率
    ​创业15年,50岁回到农村过上退休的生活,上班和创业是两难的选择。
    集成学习-Boosting
    grafana docker安装
    vue+vite项目静态文件引用丢失
  • 原文地址:https://blog.csdn.net/qq_17805707/article/details/133873954