leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
给出前序遍历和中序遍历,然后求这棵树。
很有规律。递归就可以实现。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) { int len = preorder.length;
if( len == 0)
return null;
return helper(preorder,0,len-1,inorder,0,len-1); } public TreeNode helper( int[] preorder,int pre_start,int pre_end,int[] inorder,int in_start,int in_end){ if( pre_start > pre_end || in_start > in_end )
return null;
TreeNode node = new TreeNode(preorder[pre_start]); int size = 0;
for( int i = in_start;i<=in_end && inorder[i] != preorder[pre_start];i++,size++)
;
node.left = helper(preorder,pre_start+1,pre_start+size,inorder,in_start,in_start+size-1); node.right = helper(preorder,pre_start+size+1,pre_end,inorder,in_start+size+1,in_end); return node; }
}
速度不算快,速度最快的答案也进行了参考。
并不是算法有多好,只是他在
for( int i = in_start;i<=in_end && inorder[i] != preorder[pre_start];i++,size++)
;
这里遍历的时候选择了从后向前遍历,由于测试数据的特殊性,导致了其答案的快速性。
leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java的更多相关文章
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
- leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树
不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
随机推荐
- include指令和<jsp:include>标准动作
利用JSP的包含机制,可以有效的避免重复,把可重用的部分独立出去,使用include把它们包含到当前文件.JSP有两种包含机制:include指令和<jsp:include>标准动作. 1 ...
- Rhel6-heartbeat+lvs配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...
- DotNetBar v12.2.0.7 Fully Cracked
PS: 博客园的程序出现问题,导致我的博客不能访问(转到登录页),而我自己由于 Cookies 问题,一直可以访问,所以一直未发现该问题. 感谢冰河之刃告知,thx! 更新信息: http://www ...
- 【转】linux /centos 中OpenSSL升级方法详解
相关软件下载地址 Apache:http://httpd.apache.org/ Nginx:http://nginx.org/en/download.html OpenSSL:http://www. ...
- root运行chrome
os:centos7 edit file : /usr/bin/google-chrome Add "--user-data-dir" (without the quotes) a ...
- hadoop启动之后出现错误:Retrying connect to server: hadoop/192.168.73.100:9000. Already tried 0 time(s);
INFO ipc.Client: Retrying connect to server: hadoop/192.168.73.100:9000. Already tried 0 time(s); re ...
- PAT 07-2 A+B和C
有两个值得注意的地方:1.变长数组(VLA)的使用,没想到PAT上的OJ竟然支持C99,一开始不知道就没用,看了看别人的,既然,还是用吧, 它有一点我不太喜欢,它不能像一般数组那样在声明时通过赋一个0 ...
- sql server和oracle的差异
.部分SQL语句差异 (1)SQL:select top 10 * from table ORA: select * from table where rownum<11(2)SQL:S ...
- mysql 批量创建表
使用存储过程 BEGIN DECLARE `@i` int(11); DECLARE `@sqlstr` varchar(2560); SET `@i`=0; WHILE `@i` < ...
- explain分析查询
参考以下文章,在此非常感谢原作者 explain分析查询