leetcode_106_Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:js判断变量等于字符串 编辑:程序博客网 时间:2024/05/16 05:05
描述:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
思路:
1.将中序遍历序列和其对应的下标存储到一个map中,方便下面的查找
2.递归选取后序序列的倒数第一个元素作为树的根节点,然后查找根节点在后序序列中位置inorderIndex,endInorder-inorderIndex可以得到右子树的长度
3.根据右子树的长度和endPreOrder可以求出后序序列中右子树的起始位置
4.从上面可以求出左右子树的后序序列和中序序列的起始位置,递归调用建树过程即可。
代码:
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { int[] ArrPostorder;int[] ArrInorder;Map<Integer, Integer> mapInorder = new HashMap<Integer, Integer>();public TreeNode buildTree(int[] inorder, int[] postorder) {if (inorder.length == 0 || inorder == null)return null;ArrPostorder = postorder;ArrInorder = inorder;for (int i = 0; i < inorder.length; i++)mapInorder.put(inorder[i], i);int start = 0, end = postorder.length - 1;TreeNode root = new TreeNode(0);createTree(root, start, end, start, end);return root;}public void createTree(TreeNode root, int start1, int end1, int start2,int end2) {int subStart1,subStart2,subEnd1,subEnd2;int target = ArrPostorder[end2];int indexInOrder = mapInorder.get(target);int len = end1-indexInOrder;int indexPostOrder = end2-len;if (start1 <= indexInOrder - 1) {subEnd1 = indexInOrder-1;subEnd2 = indexPostOrder - 1;root.left = new TreeNode(0);createTree(root.left, start1, subEnd1, start2, subEnd2);}root.val = target;if (indexInOrder + 1 <= end1) {subStart1 = indexInOrder + 1;subStart2 = indexPostOrder;subEnd2=end2-1;root.right = new TreeNode(0);createTree(root.right, subStart1, end1, subStart2, subEnd2);}return;}}
结果:
0 0
- leetcode_106_Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode_106_Construct Binary Tree from Inorder and Postorder Traversal
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- 初学SQL使用经验积累
- Android Scroller类的详细分析
- 一个关于三体游戏的思路
- 见与不见
- 【C语言】判断一个数是否为回文数
- leetcode_106_Construct Binary Tree from Inorder and Postorder Traversal
- maven安装中的问题
- Eclipse与Tomcat搭配使用解决tomcat自动部署时默认web项目为ROOT
- Hduoj2612【BFS】
- ActiveMQ 集群搭建
- json jar 包 json-lib-2.2.3-jdk15.jar 及相关jar
- javascript IE下不能用 trim函数解决方法
- 机器学习入门神文——读懂机器学习从这里开始
- c语言与java中原始数据类型在内存中的占位