【LeetCode】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 duplicates do not exist in the tree.
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length==0||inorder.length==0)
return null;
return BuildTreeNode(preorder,0,preorder.length-1,inorder,0,inorder.length-1); } private TreeNode BuildTreeNode(int[] preorder, int prestart, int preend, int[] inorder,
int instart, int inend) {
if(prestart>preend||instart>inend)
return null;
int temp = preorder[prestart];
TreeNode root = new TreeNode(temp);
int leftlength=0; for(int i=instart;i<=inend;i++){
if(inorder[i]!=temp)
leftlength++;
else if(inorder[i]==temp)
break;
}
root.left=BuildTreeNode(preorder, prestart+1, prestart+leftlength, inorder, instart,instart+leftlength-1);
root.right=BuildTreeNode(preorder,prestart+leftlength+1,preend,inorder,instart+leftlength+1,inend);
return root;
}
}
【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【Leetcode】【Medium】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 ...
- 【树】Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...
- 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★
1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...
- [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 OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- 【leetcode刷题笔记】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 ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- LeetCode OJ--Merge Two Sorted Lists
http://oj.leetcode.com/problems/merge-two-sorted-lists/ 有序链表的归并排序 #include <iostream> using na ...
- 深入了解Entity Framework框架及访问数据的几种方式
一.前言 1.Entity Framework概要 Entity Framework是微软以ADO.NET为基础所发展出来的对象关系映射(O/R Mapping)解决方案.该框架曾经为.NET Fra ...
- Anaconda环境搭建
最近要使用Anaconda做一些机器视觉相关的开发,在此记录下Anaconda的搭建 首先去官网下载 这里我选择windows平台 由于浏览器自己下载的过慢,我这里选择用迅雷下载 没python就把那 ...
- BZOJ1009GT考试 DP + KMP + 矩陣快速冪
@[DP, KMP, 矩陣快速冪] Description 阿申准备报名参加GT考试,准考证号为\(N\)位数\(X_1 X_2 .. X_n(0 <= X_i <= 9)\),他不希望准 ...
- Nginx图片防盗链的方式
原文:http://www.open-open.com/code/view/1430750263460 location ~* \.(gif|jpg|jpeg|png|ico)$ { valid_re ...
- 如何设置tomcat服务器编码为utf-8编码
原文:http://blog.csdn.net/u014079773/article/details/52637057 在实际开发中我们经常遇到request请求的中文乱码,那么如何解决中文乱码问题? ...
- -[__NSCFString longValue]: unrecognized selector sent to instance
You can use NSString methods intValue longLongValue. 这个使用longvalue会crash报上面的错误
- 邁向IT專家成功之路的三十則鐵律 鐵律二十九 IT人富足之道-信仰
天地自然的循環法則,讓每一個人都必須經歷生.老.病.死.喜.怒.哀.樂,然而至始至終無盡的煩惱總是遠多於快樂,因此筆者深信每一個人在一生當中,都必須要有適合自己的正確信仰,他可以在你無法以物質力量來解 ...
- uva 11374 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- SilverLight-DataControls:四、The PagedCollectionView(分页的集合视图) 对象
ylbtech-SilverLight-DataControls-PagedCollectionView:The PagedCollectionView(分页的集合视图) 对象 1.A, Buildi ...