二叉树结点间的最大距离问题

  • 递归
  • 改进的写法
  • 完整测试代码

题目

image.png

递归

解析
这个也是一个二叉树的问题,分为三步:

  • 列出所有可能性;
  • 列出结点需要的信息,并整合信息(成一个结构体);
  • 改递归 ,先假设左和右都给我信息(黑盒),然后怎么利用左边和右边的信息组出来我该返回的信息,最后basecase(边界)填什么;

具体到这个题目:
第一步,列出可能性: 一个以node为头的树上,最大距离只可能来自下面三种情况:

  • 不需要经过node这个点,node的左子树上自己的最大距离;
  • 不需要经过node这个点,node的右子树上自己的最大距离;
  • 要经过node这个点,此时就是左子树的高度 +右子树的高度+ 1

第二步,确定结点需要的信息,并整合:

  • 信息一: 返回的以node为头的树的最大距离;
  • 信息二: 返回的以node为头的高度;

第三步,封装信息,写出递归

  1. public class Main {
  2. static class Node {
  3. public int value;
  4. public Node left;
  5. public Node right;
  6. public Node(int value) {
  7. this.value = value;
  8. }
  9. }
  10. static class Pair {
  11. public int max;
  12. public int h;
  13. public Pair(int max, int h) {
  14. this.max = max;
  15. this.h = h;
  16. }
  17. }
  18. static int maxDistance(Node head) {
  19. return rec(head).max;
  20. }
  21. static Pair rec(Node head) {
  22. if (head == null)
  23. return new Pair(0, 0);
  24. Pair L = rec(head.left);
  25. Pair R = rec(head.right);
  26. return new Pair(Math.max((L.h + R.h + 1), Math.max(L.max, R.max)),
  27. Math.max(L.h, R.h) + 1);
  28. }
  29. }

改进的写法

可以使用一个全局变量记录高度,然后max正常返回: 注意在java中要使用数组,是引用,这样的话就可以一直传递,不能使用一个变量

  1. static int maxDistance2(Node head) {
  2. int[] rec = new int[1];
  3. return rec2(head, rec);
  4. }
  5. static int rec2(Node head, int[] rec) {
  6. if (head == null) {
  7. rec[0] = 0;
  8. return 0;
  9. }
  10. int L = rec2(head.left, rec);
  11. int lH = rec[0];
  12. int R = rec2(head.right, rec);
  13. int rH = rec[0];
  14. rec[0] = Math.max(lH, rH) + 1;
  15. return Math.max(lH + rH + 1, Math.max(L, R));
  16. }

完整测试代码

  1. public class MaxDistance {
  2. static class Node {
  3. public int value;
  4. public Node left;
  5. public Node right;
  6. public Node(int value) {
  7. this.value = value;
  8. }
  9. }
  10. static class Pair {
  11. public int max;
  12. public int h;
  13. public Pair(int max, int h) {
  14. this.max = max;
  15. this.h = h;
  16. }
  17. }
  18. static int maxDistance(Node head) {
  19. return rec(head).max;
  20. }
  21. static Pair rec(Node head) {
  22. if (head == null) {
  23. return new Pair(0, 0);
  24. }
  25. Pair L = rec(head.left);
  26. Pair R = rec(head.right);
  27. return new Pair(Math.max((L.h + R.h + 1), Math.max(L.max, R.max)), Math.max(L.h, R.h) + 1);
  28. }
  29. static int maxDistance2(Node head) {
  30. int[] rec = new int[1];
  31. return rec2(head, rec);
  32. }
  33. static int rec2(Node head, int[] rec) {
  34. if (head == null) {
  35. rec[0] = 0;
  36. return 0;
  37. }
  38. int L = rec2(head.left, rec);
  39. int lH = rec[0];
  40. int R = rec2(head.right, rec);
  41. int rH = rec[0];
  42. rec[0] = Math.max(lH, rH) + 1;
  43. return Math.max(lH + rH + 1, Math.max(L, R));
  44. }
  45. static Node build(int[] arr, int index) {
  46. if (index >= arr.length || arr[index] == -1)
  47. return null;
  48. Node root = new Node(arr[index]);
  49. root.left = build(arr, index * 2 + 1);
  50. root.right = build(arr, index * 2 + 2);
  51. return root;
  52. }
  53. static void printTree(Node head, int height, String to, int len) {
  54. if (head == null) return;
  55. printTree(head.right, height + 1, "v", len);
  56. String val = to + head.value + to; //两边指示的字符
  57. int lenV = val.length();
  58. int lenL = (len - lenV) / 2; //左边的空格(分一半)
  59. int lenR = len - lenV - lenL; // 右边的空格
  60. System.out.println(getSpace(len * height) + getSpace(lenL) + val + getSpace(lenR));
  61. printTree(head.left, height + 1, "^", len);
  62. }
  63. //获取指定的空格
  64. static String getSpace(int len) {
  65. StringBuffer str = new StringBuffer();
  66. for (int i = 0; i < len; i++) str.append(" ");
  67. return str.toString();
  68. }
  69. public static void main(String[] args) {
  70. int[] arr = {1, 2, 3, 4, 5, 6, 7};
  71. Node head = build(arr, 0);
  72. printTree(head, 0, "H", 10);
  73. System.out.println(maxDistance(head));
  74. System.out.println(maxDistance2(head));
  75. }
  76. }

运行结果:(打印二叉树见这个博客)

image.png