Construct Binary Tree from Preorder and Inorder Traversal (medium)
来源:互联网 发布:caxa数控车床编程软件 编辑:程序博客网 时间:2024/06/07 12:10
【题目】
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
【题意】
根据先序遍历和中序遍历创建二叉树即从前序遍历和中序遍历的结果重建一颗二叉树。
【分析】
There is an example.
_______7______ / \ __10__ ___2 / \ / 4 3 _8 \ / 1 11
The preorder and inorder traversals for the binary tree above is:
preorder = {7,10,4,3,1,2,8,11}inorder = {4,10,3,1,7,11,8,2}
The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder: {7}, {10,4,3,1}, {2,8,11}
inorder: {4,10,3,1}, {7}, {11, 8,2}
_______7______ / \ {4,10,3,1} {11,8,2}
Since we alreay find that {7} will be the root, and in "inorder" sert, all the data in the left of {7} will construct the left sub-tree. And the right part will construct a right sub-tree. We can the left and right part agin based on the preorder.
2nd roundleft part right part
preorder: {10}, {4}, {3,1} {2}, {8,11}
inorder: {4}, {10}, {3,1} {11,8}, {2}
_______7______ / \ __10__ ___2 / \ / 4 {3,1} {11,8}
see that, {10} will be the root of left-sub-tree and {2} will be the root of right-sub-tree.
Same way to split {3,1} and {11,8}, yo will get the complete tree now.
_______7______ / \ __10__ ___2 / \ / 4 3 _8 \ / 1 11
So, simulate this process from bottom to top with recursion as following code.
【实现】
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public TreeNode buildTree(int[] preorder, int[] inorder) { return buildPI(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1); }<span style="white-space:pre"></span>public TreeNode buildPI(int[] preorder, int[] inorder, int p_s, int p_e, int i_s, int i_e){<span style="white-space:pre"></span>if(p_s>p_e)<span style="white-space:pre"></span>return null;<span style="white-space:pre"></span>int pivot = preorder[p_s];<span style="white-space:pre"></span>int i = i_s;<span style="white-space:pre"></span>for(;i<i_e;i++){<span style="white-space:pre"></span>if(inorder[i]==pivot)<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>TreeNode node = new TreeNode(pivot);<span style="white-space:pre"></span>int lenLeft = i-i_s;<span style="white-space:pre"></span>node.left = buildPI(preorder, inorder, p_s+1, p_s+lenLeft, i_s, i-1);<span style="white-space:pre"></span>node.right = buildPI(preorder, inorder, p_s+lenLeft+1, p_e, i+1, i_e);<span style="white-space:pre"></span>return node;<span style="white-space:pre"></span>}
0 0
- Construct Binary Tree from Preorder and Inorder Traversal (medium)
- Construct Binary Tree from Inorder and Postorder Traversal(medium)
- [Leetcode 105, Medium] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium) (cpp)
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal & Construct Binary Tree from Inorder and P
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- XML
- python下编译py成pyc和pyo
- 线段树 hdu5700 区间交
- Caffe中卷基层和全连接层训练参数个数如何确定
- 杂论--更充分地,想尽一切办法地把群众的监督作用发挥出来
- Construct Binary Tree from Preorder and Inorder Traversal (medium)
- JavaScript 加号操作符细节
- 面向对象设计原则
- 简单数据库连接池的原理
- Redis内存优化
- JCEF的编译
- html表单
- pthread_join/pthread_exit的用法解析
- 开源NFV管理器 - OpenStack Tacker介绍 - 2. Tacker DevStack安装配置和操作