105. Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:it技能 help desk 编辑:程序博客网 时间:2024/05/24 05:26
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
这道题目意思很明确:给出一棵树的先序和中序遍历(用数组形式给出),让我们构造这棵树。
我们知道,先序遍历是根左右的顺序,所以先序遍历数组的第一个元素是树的根,而中序遍历是左根右的顺序,所以可以通过先序遍历找到树的根,再根据中序遍历去找树的左右子树,然后用递归的方法去找左右子树的根和其左右子树,直到搜索完毕为止。
这里用pre_start表示preorder的开始索引,用in_start表示inorder的开始索引,in_end表示inorder的结束索引,当找到树的根在inorder中的索引index之后之后,左子树的pre_start变成pre_start+1(因为preorder的pre_start+1表示左子树的根),并且左子树在inorder中的开始和结束索引变为in_start和index-1,右子树的pre_start变为pre_start+index-instart+1,因为右子树在pre_order中的根索引计算方式为:pre_start再加上左子树的所有节点个数(index-1-in_start+1=index-in_start)再加上1,在inorder中的开始和结束索引分别为index+1和in_end,因为每次遍历的节点都用TreeNode root保存,所以最后返回root即可,代码如下:
public class BuildTree {public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public TreeNode buildTree(int[] preorder, int[] inorder) { return help(0,0,inorder.length-1,preorder,inorder); } public TreeNode help(int pre_start,int in_start,int in_end,int[] preorder, int[] inorder){ if(pre_start>preorder.length-1||in_start>in_end){ return null; } TreeNode root = new TreeNode(preorder[pre_start]); int index = 0; for(int i=in_start;i<=in_end;i++){ if(inorder[i]==root.val){ index = i; } } root.left = help(pre_start+1,in_start,index-1,preorder,inorder); root.right = help(pre_start+index-in_start+1,index+1,in_end,preorder,inorder); return root; }}
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode --- 105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] 105.Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal LeetCode
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode#105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- Android实现按钮点击传按钮的值到另一个按钮
- 自己写的HTML嵌入Android项目,运用webview控件与Fragment与activity之间的传值
- 线程本地存储ThreadLocal
- linux修改镜像的地址
- 在ubuntu和QT中使用OpenCV
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 统计一个数字在排序数组中出现的次数
- 简单的MD5加密
- CSS3的新增选择器示例-属性选择器
- 设置完成历程
- 自定义控件设置canvas画布的大小
- iOS编程学习八
- PHP中利用PHPMailer实现发邮件
- 《UNIX环境高级编程》第17章 高级进程间通信