[leetcode] Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
思路:递归求解,后序遍历可以获得根节点,然后根据根节点在中序排列中的位置分出左右子树。时间复杂度O(N*N)
时间优化:可以把节点放到map中,加快在inorder中搜索的速度。 O(N)
public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if (postorder.length == 0 || inorder.length == 0) return null; TreeNode res = build(inorder, postorder, inorder.length); return res; } private TreeNode build(int[] in, int[] post, int len) { if (len <= 0) return null; TreeNode root = new TreeNode(post[len - 1]); int idx = -1; for (int i = 0; i < len; i++) { if (in[i] == post[len - 1]) idx = i; } root.left = build(Arrays.copyOfRange(in, 0, idx), Arrays.copyOfRange(post, 0, idx), idx); root.right = build(Arrays.copyOfRange(in, idx + 1, len), Arrays.copyOfRange(post, idx, len - 1), len - 1 - idx); return root; } public static void main(String[] args) { new Solution().buildTree(new int[] { 9, 3, 15, 20, 7 }, new int[] { 9, 15, 7, 20, 3 }); } }
时空O(N) 解法
class Solution { int postIdx; int[] postorder; int[] inorder; Map<Integer,Integer> idxMap = new HashMap<>(); public TreeNode buildTree(int[] inorder, int[] postorder) { this.postorder = postorder; this.inorder = inorder; postIdx = inorder.length-1; for(int i=0;i<inorder.length;i++){ idxMap.put(inorder[i],i); } return helper(0, inorder.length-1); } public TreeNode helper(int inLeft, int inRight){ if(inLeft>inRight){ return null; } int rootVal = postorder[postIdx]; TreeNode root = new TreeNode(rootVal); int idx = idxMap.get(rootVal); postIdx--; root.right = helper(idx+1,inRight); root.left = helper(inLeft,idx-1); return root; } }
不创建新的数组:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if(inorder == null || postorder == null) return null; return build(inorder,0,inorder.length,postorder,0,postorder.length); } private TreeNode build(int[] in, int a, int b, int[] post, int c, int d){ if(a>=b) return null; int rootIdx = -1; for(int i=a;i<b;i++){ if(in[i]==post[d-1]){ rootIdx = i; } } int leftLen = rootIdx-a; TreeNode root = new TreeNode(post[d-1]); root.left = build(in,a,rootIdx,post,c,c+leftLen); root.right = build(in,rootIdx+1,b,post,c+leftLen,d-1); return root; } }
利用Map O(N)算法
class Solution { Map<Integer,Integer> idxMap = new HashMap<>(); public TreeNode buildTree(int[] inorder, int[] postorder) { if(inorder == null || postorder == null) return null; for(int i=0;i<inorder.length;i++){ idxMap.put(inorder[i],i); } return build(inorder,0,inorder.length-1,postorder,0,postorder.length-1); } private TreeNode build(int[] in, int a, int b, int[] post, int c, int d){ if(a>b) return null; int rootIdx = idxMap.get(post[d]); int leftLen = rootIdx-a; TreeNode root = new TreeNode(post[d]); root.left = build(in,a,rootIdx-1,post,c,c+leftLen-1); root.right = build(in,rootIdx+1,b,post,c+leftLen,d-1); return root; } }
第二遍记录:
java的写法,数组要拷贝,注意一般java边界是exclusive的
注意递归的边界(即数组长度为0时),开始忘记了。
public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if(inorder == null || postorder == null) return null; return build(inorder,postorder); } private TreeNode build(int[] inorder, int[] postorder){ if(inorder==null||inorder.length==0) return null; int rootVal = postorder[postorder.length-1]; int idx=-1; for(int i=0;i<inorder.length;i++){ if(inorder[i]==rootVal){ idx=i; break; } } TreeNode root = new TreeNode(rootVal); root.left= build(Arrays.copyOfRange(inorder,0,idx),Arrays.copyOfRange(postorder,0,idx)); root.right=build(Arrays.copyOfRange(inorder,idx+1,inorder.length),Arrays.copyOfRange(postorder,idx,postorder.length-1)); return root; } }