給定一個二叉樹的頭結點,返回最大搜索子樹的大小。
?
我們先定義結點:
public static class Node {public int value;public Node left;public Node right;public Node(int data) {this.value = data;}}
分析:
直接判斷每個節點左邊小右邊大是不對滴
?
可以暴力判斷所有的子樹,就不說了。
?
最大搜索子樹可能性:
第一種可能性,以node為頭的結點的最大二叉搜索子樹可能來自它左子樹;
第二種可能性,以node為頭的結點的最大二叉搜索子樹可能來自它右子樹;
第三種可能性,左樹整體是搜索二叉樹,右樹整體也是搜索二叉樹,而且左樹的頭是node.left,右樹的頭是node.right,且左樹的最大值< node.value,右樹的最小值 > node.value, 那么以我為頭的整棵樹都是搜索二叉樹;
?
第三種可能性的判斷,需要的信息有:左子樹的最大值、右子樹的最小值、左子樹是不是搜索二叉樹、右子樹是不是搜索二叉樹
還有左右搜索二叉樹的最大深度。
我們判斷了自己,并不知道自己是哪邊的子樹,我們要返回自己的最大值和最小值。
這樣,定義一個返回類型:
public static class ReturnType{public int size;//最大搜索子樹深度public Node head;//最大搜索子樹的根public int min;//子樹最小public int max;//子樹最大public ReturnType(int a, Node b,int c,int d) {this.size =a;this.head = b;this.min = c;this.max = d;}}
然后開始寫代碼:
注意:
1)NULL返回深度0,頭為NULL,最大值最小值返回系統最大和最小,這樣才不會影響別的判斷。
public static ReturnType process(Node head) {if(head == null) {return new ReturnType(0,null,Integer.MAX_VALUE, Integer.MIN_VALUE);}Node left = head.left;//取信息ReturnType leftSubTressInfo = process(left);Node right = head.right;ReturnType rightSubTressInfo = process(right);int includeItSelf = 0;if(leftSubTressInfo.head == left // 左子樹為搜索樹&&rightSubTressInfo.head == right// 右子樹為搜索樹&& head.value > leftSubTressInfo.max// 左子樹最大值小于當前節點&& head.value < rightSubTressInfo.min//右子樹最小值大于當前節點) {includeItSelf = leftSubTressInfo.size + 1 + rightSubTressInfo.size;//當前節點為根的二叉樹為搜索樹}int p1 = leftSubTressInfo.size;int p2 = rightSubTressInfo.size;int maxSize = Math.max(Math.max(p1, p2), includeItSelf);//最大搜索樹深度Node maxHead = p1 > p2 ? leftSubTressInfo.head : rightSubTressInfo.head;if(maxSize == includeItSelf) {maxHead = head;}//最大搜索樹的根:來自左子樹、來自右子樹、本身return new ReturnType(maxSize, //深度maxHead, //根Math.min(Math.min(leftSubTressInfo.min,rightSubTressInfo.min),head.value), //最小Math.max(Math.max(leftSubTressInfo.max,rightSubTressInfo.max),head.value)); //最大}
可以進一步改進:
空間浪費比較嚴重
其實返回值為三個int,一個node,我們可以把三個int合起來,用全局數組記錄,函數只返回node(搜索樹的根)即可。
給出完整代碼:
public class BiggestSubBSTInTree {public static class Node {public int value;public Node left;public Node right;public Node(int data) {this.value = data;}}public static Node biggestSubBST(Node head) {int[] record = new int[3]; // 0->size, 1->min, 2->maxreturn posOrder(head, record);}public static class ReturnType{public int size;//最大搜索子樹深度public Node head;//最大搜索子樹的根public int min;//子樹最小public int max;//子樹最大public ReturnType(int a, Node b,int c,int d) {this.size =a;this.head = b;this.min = c;this.max = d;}}public static ReturnType process(Node head) {if(head == null) {return new ReturnType(0,null,Integer.MAX_VALUE, Integer.MIN_VALUE);}Node left = head.left;//取信息ReturnType leftSubTressInfo = process(left);Node right = head.right;ReturnType rightSubTressInfo = process(right);int includeItSelf = 0;if(leftSubTressInfo.head == left // 左子樹為搜索樹&&rightSubTressInfo.head == right// 右子樹為搜索樹&& head.value > leftSubTressInfo.max// 左子樹最大值小于當前節點&& head.value < rightSubTressInfo.min//右子樹最小值大于當前節點) {includeItSelf = leftSubTressInfo.size + 1 + rightSubTressInfo.size;//當前節點為根的二叉樹為搜索樹}int p1 = leftSubTressInfo.size;int p2 = rightSubTressInfo.size;int maxSize = Math.max(Math.max(p1, p2), includeItSelf);//最大搜索樹深度Node maxHead = p1 > p2 ? leftSubTressInfo.head : rightSubTressInfo.head;if(maxSize == includeItSelf) {maxHead = head;}//最大搜索樹的根:來自左子樹、來自右子樹、本身return new ReturnType(maxSize, //深度maxHead, //根Math.min(Math.min(leftSubTressInfo.min,rightSubTressInfo.min),head.value), //最小Math.max(Math.max(leftSubTressInfo.max,rightSubTressInfo.max),head.value)); //最大}public static Node posOrder(Node head, int[] record) {if (head == null) {record[0] = 0;record[1] = Integer.MAX_VALUE;record[2] = Integer.MIN_VALUE;return null;}int value = head.value;Node left = head.left;Node right = head.right;Node lBST = posOrder(left, record);int lSize = record[0];int lMin = record[1];int lMax = record[2];Node rBST = posOrder(right, record);int rSize = record[0];int rMin = record[1];int rMax = record[2];record[1] = Math.min(rMin, Math.min(lMin, value)); // lmin, value, rmin -> min record[2] = Math.max(lMax, Math.max(rMax, value)); // lmax, value, rmax -> maxif (left == lBST && right == rBST && lMax < value && value < rMin) {record[0] = lSize + rSize + 1;//修改深度return head; //返回根}//滿足當前構成搜索樹的條件record[0] = Math.max(lSize, rSize);//較大深度return lSize > rSize ? lBST : rBST;//返回較大搜索樹的根}// for test -- print treepublic static void printTree(Node head) {System.out.println("Binary Tree:");printInOrder(head, 0, "H", 17);System.out.println();}public static void printInOrder(Node head, int height, String to, int len) {if (head == null) {return;}printInOrder(head.right, height + 1, "v", len);String val = to + head.value + to;int lenM = val.length();int lenL = (len - lenM) / 2;int lenR = len - lenM - lenL;val = getSpace(lenL) + val + getSpace(lenR);System.out.println(getSpace(height * len) + val);printInOrder(head.left, height + 1, "^", len);}public static String getSpace(int num) {String space = " ";StringBuffer buf = new StringBuffer("");for (int i = 0; i < num; i++) {buf.append(space);}return buf.toString();}public static void main(String[] args) {Node head = new Node(6);head.left = new Node(1);head.left.left = new Node(0);head.left.right = new Node(3);head.right = new Node(12);head.right.left = new Node(10);head.right.left.left = new Node(4);head.right.left.left.left = new Node(2);head.right.left.left.right = new Node(5);head.right.left.right = new Node(14);head.right.left.right.left = new Node(11);head.right.left.right.right = new Node(15);head.right.right = new Node(13);head.right.right.left = new Node(20);head.right.right.right = new Node(16);printTree(head);Node bst = biggestSubBST(head);printTree(bst);}}
?