/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/classCodec{// Encodes a tree to a single string.publicStringserialize(TreeNode root){if(root ==null)return"";StringBuilder s =newStringBuilder();postOrder(root, s);return s.substring(0, s.length()-1);// 移除末尾 ','}publicvoidpostOrder(TreeNode node,StringBuilder s){if(node ==null)return;postOrder(node.left, s);postOrder(node.right, s);s.append(node.val).append(",");}// Decodes your encoded data to tree.publicTreeNodedeserialize(String data){if(data.equals(""))returnnull;String[] ss = data.split(",");int[] arr =newint[ss.length];for(int i =0; i < ss.length; i++){arr[i]=Integer.parseInt(ss[i]);}returnbuildBST(arr,0, arr.length -1);}// 根據(jù)后序遍歷BST性質(zhì),頭結(jié)點(diǎn)是最后一個(gè)元素,且有一個(gè)分界,滿足比頭結(jié)點(diǎn)小的都在左邊,比頭結(jié)點(diǎn)大的都在右邊publicTreeNodebuildBST(int[] arr,intL,intR){if(L>R)returnnull;TreeNode node =newTreeNode(arr[R]);// 找左右子樹的分界點(diǎn)intM=L;while(arr[M]< arr[R]){M++;}// M指向分界點(diǎn)右邊第一個(gè)元素,本題缺隱含條件:沒有重復(fù)值!node.left =buildBST(arr,L,M-1);node.right =buildBST(arr,M,R-1);return node;}}// Your Codec object will be instantiated and called as such:// Codec ser = new Codec();// Codec deser = new Codec();// String tree = ser.serialize(root);// TreeNode ans = deser.deserialize(tree);// return ans;
可參考:左程云《程序員代碼面試指南》根據(jù)后序數(shù)組重建搜索二叉樹
packagechapter_3_binarytreeproblem;publicclassProblem_14_PosArrayToBST{publicstaticbooleanisPostArray(int[] arr){if(arr ==null|| arr.length ==0){returnfalse;}returnisPost(arr,0, arr.length -1);}publicstaticbooleanisPost(int[] arr,int start,int end){if(start == end){returntrue;}int less =-1;int more = end;for(int i = start; i < end; i++){if(arr[end]> arr[i]){less = i;}else{more = more == end ? i : more;}}if(less ==-1|| more == end){returnisPost(arr, start, end -1);}if(less != more -1){returnfalse;}returnisPost(arr, start, less)&&isPost(arr, more, end -1);}publicstaticclassNode{publicint value;publicNode left;publicNode right;publicNode(int value){this.value = value;}}publicstaticNodeposArrayToBST(int[] posArr){if(posArr ==null){returnnull;}returnposToBST(posArr,0, posArr.length -1);}publicstaticNodeposToBST(int[] posArr,int start,int end){if(start > end){returnnull;}Node head =newNode(posArr[end]);int less =-1;int more = end;for(int i = start; i < end; i++){if(posArr[end]> posArr[i]){less = i;}else{more = more == end ? i : more;}}head.left =posToBST(posArr, start, less);head.right =posToBST(posArr, more, end -1);return head;}// for test -- print treepublicstaticvoidprintTree(Node head){System.out.println("Binary Tree:");printInOrder(head,0,"H",17);System.out.println();}publicstaticvoidprintInOrder(Node head,int height,Stringto,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);}publicstaticStringgetSpace(int num){String space =" ";StringBuffer buf =newStringBuffer("");for(int i =0; i < num; i++){buf.append(space);}return buf.toString();}publicstaticvoidmain(String[] args){int[] arr ={2,1,3,6,5,7,4};System.out.println(isPost(arr,0, arr.length -1));printTree(posArrayToBST(arr));}}