标签:
跟另一道题一样,不多说了
public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { // post就是pre的近似倒过来 if(inorder==null|| postorder==null||inorder.length==0||postorder.length==0) return null; return buildTree(postorder, 0, postorder.length-1, inorder, 0, inorder.length-1); } public TreeNode buildTree(int[] p, int p1, int p2, int[] in, int in1, int in2 ){ if(p2<p1||in2<in1) return null; int len=0; while(in[in1+len]!=p[p2]){ len++; } TreeNode root = new TreeNode(p[p2]); root.left = buildTree(p,p1,p1+len-1,in,in1, in1+len-1); root.right = buildTree(p,p1+len,p2-1,in, in1+len+1,in2); return root; } }
Construct Binary Tree from Inorder and Postorder Traversal
标签:
原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4436535.html