leetcode105: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.
解题思路分析:
前序遍历首先遍历根节点,然后依次遍历左右子树
中序遍历首先遍历左子树,然后遍历根结点,最后遍历右子树
根据二者的特点,前序遍历的首节点就是根结点,然后在中序序列中找出该结点位置(index),则该结点之前的为左子树结点,该节点之后为右子树结点;
同样对于前序序列,首结点之后的index个结点为左子树结点,剩余的节点为右子树结点;
最后,递归建立子树即可。
java代码:
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder == null || preorder.length == 0){
return null;
}
int flag = preorder[0];
TreeNode root = new TreeNode(flag);
int i = 0;
for(i=0; i<inorder.length; i++){
if(flag == inorder[i]){
break;
}
}
int[] pre_left, pre_right, in_left, in_right;
pre_left = new int[i];
for(int j=1; j<=i;j++){
pre_left[j-1] = preorder[j];
}
pre_right = new int[preorder.length-i-1];
for(int j=i+1; j<preorder.length;j++){
pre_right[j-i-1] = preorder[j];
}
in_left = new int[i];
for(int j=0;j<i;j++){
in_left[j] = inorder[j];
}
in_right = new int[inorder.length-i-1];
for(int j=i+1; j<inorder.length; j++){
in_right[j-i-1] = inorder[j];
}
root.left = buildTree(pre_left,in_left);
root.right = buildTree(pre_right,in_right);
return root;
}
}
leetcode105:Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- LeetCode OJ: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 ...
- 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★
1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【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 ...
- [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 ...
随机推荐
- [Cocos2d-x For WP8]ActionManager动作管理
在Cocos2d-x里面可以通过CCActionManger类来管理动作的暂停和恢复,CCActionMessage是管理所有Action的单例,一般情况下并不直接使用这个单例,而是使用CCNode的 ...
- 【poj2828】Buy Tickets
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- 《少有人走的路:心智成熟的旅程》--[美]M·斯科特·派克
<少有人走的路>,美国作家M·斯科特·派克所著 下面是我的书摘: * 归根到底,它告诉我们怎样找到真正的自我. * 人可以拒绝任何东西,但绝对不可以决绝成熟.决绝成熟,实际上就是在规避问题 ...
- 人工智能 --test
http://zhidao.baidu.com/link?url=9qp_SbSRfzMezkD25FZiWyNDsMxgcK6lecYYt0SW1ESsqkRaV5LYQ-0ysk3F2e35ajA ...
- If A wants to use B
Find the place where B is used, and use C to call C.B RootFrame.Navigated += CheckForResetNavigation ...
- SQL中的with as
一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让 ...
- android-GridView控件的使用
GridView 按行列方式显示多个组件(二维布局界面) 数据源(集合)-适配器(SimpleAdapter)-视图界面(GridView),加载适配器-配置监听器(OnItemClickListen ...
- Golden Gate 概念和机制
1. OGG有哪些进程 ü Manger : manger进程是goldengate的控制进程,分别运行在源端和目标端上,它主要的作用是启动.监控.重启goldengate的其他进程,报告错误及事件 ...
- websocket总结
一.WebSocket简介 WebSocket protocol是HTML5一种新的协议,WebSocket 是目前唯一真正实现全双工通信的服务器向客户端推送的互联网技术.WebSocket的出现使 ...
- C++产生随机数四则运算
产生两位随机整数,随机四则运算符,生成30道运算题. 一.编程思路 看到要求,首先想到的是怎么运用随机数,因为自己对随机数的不熟练所以还要在查很多东西.在一个for循环内先产生两个30以内的随机数,在 ...