[LeetCode] Construct/build binary tree from in-order and post-order/pre-order traversal
2015-04-07 11:23
465 查看
解答从这儿来:http://blog.csdn.net/linhuanmars/article/details/24390157 和 http://blog.csdn.net/linhuanmars/article/details/24389549
in-order 和 pre-order
假设树的先序遍历是12453687,中序遍历是42516837。
这里最重要的一点就是先序遍历可以提供根的所在,而根据中序遍历的性质知道根的所在就可以将序列分为左右子树。
比如上述例子,我们知道1是根,所以根据中序遍历的结果425是左子树,而6837就是右子树。接下来根据切出来的左右子树的长度又可以在先序便利中确定左右子树对应的子序列(先序遍历也是先左子树后右子树)。根据这个流程,左子树的先序遍历和中序遍历分别是245和425,右子树的先序遍历和中序遍历则是3687和6837,
我们重复以上方法,可以继续找到根和左右子树,直到剩下一个元素。
可以看出这是一个比较明显的递归过程,对于寻找根所对应的下标,我们可以先建立一个HashMap,以免后面需要进行线行搜索,这样每次递归中就只需要常量操作就可以完成对根的确定和左右子树的分割。
in-order 和 post-order
这里的区别是要从中序遍历和后序遍历中构造出树,算法还是一样,只是现在取根是从后面取(因为后序遍历根是遍历的最后一个元素)
in-order: 4 2 5 / (1) / 6 7 3 8
post-order: 4 5 2/ 6 7 8 3 / (1)
in-order 和 pre-order
假设树的先序遍历是12453687,中序遍历是42516837。
这里最重要的一点就是先序遍历可以提供根的所在,而根据中序遍历的性质知道根的所在就可以将序列分为左右子树。
比如上述例子,我们知道1是根,所以根据中序遍历的结果425是左子树,而6837就是右子树。接下来根据切出来的左右子树的长度又可以在先序便利中确定左右子树对应的子序列(先序遍历也是先左子树后右子树)。根据这个流程,左子树的先序遍历和中序遍历分别是245和425,右子树的先序遍历和中序遍历则是3687和6837,
我们重复以上方法,可以继续找到根和左右子树,直到剩下一个元素。
可以看出这是一个比较明显的递归过程,对于寻找根所对应的下标,我们可以先建立一个HashMap,以免后面需要进行线行搜索,这样每次递归中就只需要常量操作就可以完成对根的确定和左右子树的分割。
public TreeNode buildTree(int[] preorder, int[] inorder) { if(preorder==null || inorder==null) return null; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i=0;i<inorder.length;i++) //initializing the hashmap for in-order lookup { map.put(inorder[i],i); } return helper(preorder,0,preorder.length-1,inorder,0,inorder.length-1, map); } private TreeNode helper(int[] preorder, int preL, int preR, int[] inorder, int inL, int inR, HashMap<Integer, Integer> map) { if(preL>preR || inL>inR) return null; TreeNode root = new TreeNode(preorder[preL]); int index = map.get(root.val); //remember here the length of left tree is index-inL, where index is from in-order root.left = helper(preorder, preL+1, index-inL+preL, inorder, inL, index-1, map); root.right = helper(preorder, preL+index-inL+1, preR, inorder, index+1, inR,map); return root; }
in-order 和 post-order
这里的区别是要从中序遍历和后序遍历中构造出树,算法还是一样,只是现在取根是从后面取(因为后序遍历根是遍历的最后一个元素)
in-order: 4 2 5 / (1) / 6 7 3 8
post-order: 4 5 2/ 6 7 8 3 / (1)
public TreeNode buildTree(int[] inorder, int[] postorder) { if(inorder==null || postorder==null || inorder.length==0 || postorder.length==0) { return null; } HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i=0;i<inorder.length;i++) { map.put(inorder[i],i); } return helper(inorder,postorder,0,inorder.length-1, 0, postorder.length-1,map); } private TreeNode helper(int[] inorder, int[] postorder, int inL, int inR, int postL, int postR, HashMap<Integer, Integer> map) { if(inL>inR || postL>postR) return null; TreeNode root = new TreeNode(postorder[postR]); int index = map.get(root.val); //remember here the length of left tree is index-inL, where index is from in-order root.left = helper(inorder,postorder,inL,index-1,postL,postL+index-inL-1,map); root.right = helper(inorder,postorder,index+1,inR,postR-(inR-index),postR-1,map); return root; }
相关文章推荐
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder/Preorder and Inorder Traversal
- leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal
- 【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal
- leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal
- 【LeetCode】Construct Binary Tree from (Preorder or Postorder) and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 105, 106. Construct Binary Tree from Preorder and Inorder/Inorder and Postorder Traversal
- [Leetcode][python]Construct Binary Tree from Preorder and Inorder Traversal (Inorder and Postorder)
- leetcode - Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
- leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal 分类: leetCode 2015-06-22 08:26 177人阅读 评论(0) 收藏
- LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode_Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode_Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- leetcode - Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal