二叉树最长路径

举报
兔老大 发表于 2021/04/28 01:24:19 2021/04/28
【摘要】 分析: 暴力求每一段距离也可。   对于以本节点为根的二叉树,最远距离有三种可能: 1)最远路径来自左子树 2 )最远路径来自右子树(图示与左子树同理) 3)最远路径为左右子树距离根最远的两个节点,经过根结点连起来。 (多种最长路径)   需要的信息: 1)左子树的最远路径长度 2)右子树的最远路径长度 3)左右子树的深度(...

分析:

暴力求每一段距离也可。

 

对于以本节点为根的二叉树,最远距离有三种可能:

1)最远路径来自左子树

2 )最远路径来自右子树(图示与左子树同理)

3)最远路径为左右子树距离根最远的两个节点,经过根结点连起来。

(多种最长路径)

 

需要的信息:

1)左子树的最远路径长度

2)右子树的最远路径长度

3)左右子树的深度(深度即最远节点)

 

定义结点:


  
  1. public static class Node {
  2. public int value;
  3. public Node left;
  4. public Node right;
  5. public Node(int data) {
  6. this.value = data;
  7. }
  8. }

构造返回值信息:


  
  1. public static class ReturnType{
  2. public int maxDistance;//最长距离
  3. public int h; //高度
  4. public ReturnType(int m, int h) {
  5. this.maxDistance = m;;
  6. this.h = h;
  7. }
  8. }

求解过程比较好写了:


  
  1. public static ReturnType process(Node head) {
  2. if(head == null) {
  3. return new ReturnType(0,0);
  4. }
  5. //收信息
  6. ReturnType leftReturnType = process(head.left);
  7. ReturnType rightReturnType = process(head.right);
  8. int includeHeadDistance = leftReturnType.h + 1 + rightReturnType.h;//情况3
  9. int p1 = leftReturnType.maxDistance;
  10. int p2 = rightReturnType.maxDistance;
  11. int resultDistance = Math.max(Math.max(p1, p2), includeHeadDistance);//最长距离
  12. int hitself = Math.max(leftReturnType.h, leftReturnType.h) + 1; //树的高度等于子树高度+1
  13. return new ReturnType(resultDistance, hitself);
  14. }

优化:

其实我们不需要返回左右子树深度,可以用一个全局变量记录。遇到NULL把变量记为0,不影响接下来的计算。

用一个含一个元素的数组来记录。因为某些二叉树题目不只需要一个信息,所以要利用全局数组。


  
  1. public static int posOrder(Node head, int[] record) {
  2. if (head == null) {
  3. record[0] = 0;//重要
  4. return 0;
  5. }
  6. //取信息
  7. int lMax = posOrder(head.left, record);
  8. int maxfromLeft = record[0];
  9. int rMax = posOrder(head.right, record);
  10. int maxFromRight = record[0];
  11. int curNodeMax = maxfromLeft + maxFromRight + 1;//情况3
  12. record[0] = Math.max(maxfromLeft, maxFromRight) + 1;
  13. return Math.max(Math.max(lMax, rMax), curNodeMax);
  14. }

最后放上全部代码:


  
  1. package q;
  2. public class Demo {
  3. public static class Node {
  4. public int value;
  5. public Node left;
  6. public Node right;
  7. public Node(int data) {
  8. this.value = data;
  9. }
  10. }
  11. public static int maxDistance(Node head) {
  12. int[] record = new int[1];
  13. return posOrder(head, record);
  14. }
  15. public static class ReturnType{
  16. public int maxDistance;//最长距离
  17. public int h; //高度
  18. public ReturnType(int m, int h) {
  19. this.maxDistance = m;;
  20. this.h = h;
  21. }
  22. }
  23. public static ReturnType process(Node head) {
  24. if(head == null) {
  25. return new ReturnType(0,0);
  26. }
  27. //收信息
  28. ReturnType leftReturnType = process(head.left);
  29. ReturnType rightReturnType = process(head.right);
  30. int includeHeadDistance = leftReturnType.h + 1 + rightReturnType.h;//情况3
  31. int p1 = leftReturnType.maxDistance;
  32. int p2 = rightReturnType.maxDistance;
  33. int resultDistance = Math.max(Math.max(p1, p2), includeHeadDistance);//最长距离
  34. int hitself = Math.max(leftReturnType.h, leftReturnType.h) + 1; //树的高度等于子树高度+1
  35. return new ReturnType(resultDistance, hitself);
  36. }
  37. public static int posOrder(Node head, int[] record) {
  38. if (head == null) {
  39. record[0] = 0;//重要
  40. return 0;
  41. }
  42. //取信息
  43. int lMax = posOrder(head.left, record);
  44. int maxfromLeft = record[0];
  45. int rMax = posOrder(head.right, record);
  46. int maxFromRight = record[0];
  47. int curNodeMax = maxfromLeft + maxFromRight + 1;//情况3
  48. record[0] = Math.max(maxfromLeft, maxFromRight) + 1;
  49. return Math.max(Math.max(lMax, rMax), curNodeMax);
  50. }
  51. public static void main(String[] args) {
  52. Node head1 = new Node(1);
  53. head1.left = new Node(2);
  54. head1.right = new Node(3);
  55. head1.left.left = new Node(4);
  56. head1.left.right = new Node(5);
  57. head1.right.left = new Node(6);
  58. head1.right.right = new Node(7);
  59. head1.left.left.left = new Node(8);
  60. head1.right.left.right = new Node(9);
  61. System.out.println(maxDistance(head1));
  62. Node head2 = new Node(1);
  63. head2.left = new Node(2);
  64. head2.right = new Node(3);
  65. head2.right.left = new Node(4);
  66. head2.right.right = new Node(5);
  67. head2.right.left.left = new Node(6);
  68. head2.right.right.right = new Node(7);
  69. head2.right.left.left.left = new Node(8);
  70. head2.right.right.right.right = new Node(9);
  71. System.out.println(maxDistance(head2));
  72. }
  73. }

 

文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。

原文链接:fantianzuo.blog.csdn.net/article/details/84254502

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。