分析:
暴力求每一段距離也可。
?
對于以本節點為根的二叉樹,最遠距離有三種可能:
1)最遠路徑來自左子樹
2 )最遠路徑來自右子樹(圖示與左子樹同理)
3)最遠路徑為左右子樹距離根最遠的兩個節點,經過根結點連起來。
(多種最長路徑)
?
需要的信息:
1)左子樹的最遠路徑長度
2)右子樹的最遠路徑長度
3)左右子樹的深度(深度即最遠節點)
?
定義結點:
public static class Node {public int value;public Node left;public Node right;public Node(int data) {this.value = data;}}
構造返回值信息:
public static class ReturnType{public int maxDistance;//最長距離public int h; //高度public ReturnType(int m, int h) {this.maxDistance = m;;this.h = h;}}
求解過程比較好寫了:
public static ReturnType process(Node head) {if(head == null) {return new ReturnType(0,0);}//收信息ReturnType leftReturnType = process(head.left);ReturnType rightReturnType = process(head.right);int includeHeadDistance = leftReturnType.h + 1 + rightReturnType.h;//情況3int p1 = leftReturnType.maxDistance;int p2 = rightReturnType.maxDistance;int resultDistance = Math.max(Math.max(p1, p2), includeHeadDistance);//最長距離int hitself = Math.max(leftReturnType.h, leftReturnType.h) + 1; //樹的高度等于子樹高度+1return new ReturnType(resultDistance, hitself);}
優化:
其實我們不需要返回左右子樹深度,可以用一個全局變量記錄。遇到NULL把變量記為0,不影響接下來的計算。
用一個含一個元素的數組來記錄。因為某些二叉樹題目不只需要一個信息,所以要利用全局數組。
public static int posOrder(Node head, int[] record) {if (head == null) {record[0] = 0;//重要return 0;}//取信息int lMax = posOrder(head.left, record);int maxfromLeft = record[0];int rMax = posOrder(head.right, record);int maxFromRight = record[0];int curNodeMax = maxfromLeft + maxFromRight + 1;//情況3record[0] = Math.max(maxfromLeft, maxFromRight) + 1;return Math.max(Math.max(lMax, rMax), curNodeMax);}
最后放上全部代碼:
package q;public class Demo {public static class Node {public int value;public Node left;public Node right;public Node(int data) {this.value = data;}}public static int maxDistance(Node head) {int[] record = new int[1];return posOrder(head, record);}public static class ReturnType{public int maxDistance;//最長距離public int h; //高度public ReturnType(int m, int h) {this.maxDistance = m;;this.h = h;}}public static ReturnType process(Node head) {if(head == null) {return new ReturnType(0,0);}//收信息ReturnType leftReturnType = process(head.left);ReturnType rightReturnType = process(head.right);int includeHeadDistance = leftReturnType.h + 1 + rightReturnType.h;//情況3int p1 = leftReturnType.maxDistance;int p2 = rightReturnType.maxDistance;int resultDistance = Math.max(Math.max(p1, p2), includeHeadDistance);//最長距離int hitself = Math.max(leftReturnType.h, leftReturnType.h) + 1; //樹的高度等于子樹高度+1return new ReturnType(resultDistance, hitself);}public static int posOrder(Node head, int[] record) {if (head == null) {record[0] = 0;//重要return 0;}//取信息int lMax = posOrder(head.left, record);int maxfromLeft = record[0];int rMax = posOrder(head.right, record);int maxFromRight = record[0];int curNodeMax = maxfromLeft + maxFromRight + 1;//情況3record[0] = Math.max(maxfromLeft, maxFromRight) + 1;return Math.max(Math.max(lMax, rMax), curNodeMax);}public static void main(String[] args) {Node head1 = new Node(1);head1.left = new Node(2);head1.right = new Node(3);head1.left.left = new Node(4);head1.left.right = new Node(5);head1.right.left = new Node(6);head1.right.right = new Node(7);head1.left.left.left = new Node(8);head1.right.left.right = new Node(9);System.out.println(maxDistance(head1));Node head2 = new Node(1);head2.left = new Node(2);head2.right = new Node(3);head2.right.left = new Node(4);head2.right.right = new Node(5);head2.right.left.left = new Node(6);head2.right.right.right = new Node(7);head2.right.left.left.left = new Node(8);head2.right.right.right.right = new Node(9);System.out.println(maxDistance(head2));}}
?