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 duplicates do not exist in the tree.
从前序以及中序的结果中构造二叉树,这里保证了不会有两个相同的数字,用递归构造就比较方便了:
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(!preorder.size()) return NULL;
return createTree(preorder, , preorder.size() - , inorder, , inorder.size() - );//边界条件应该想清楚
} TreeNode* createTree(vector<int>& preOrder, int preBegin, int preEnd, vector<int>& inOrder, int inBegin, int inEnd)
{
if(preBegin > preEnd) return NULL;
int rootVal = preOrder[preBegin];
int mid;
for(int i = inBegin; i <= inEnd; ++i)
if(inOrder[i] == rootVal){
mid = i;
break;
}
int len = mid - inBegin; //左边区间的长度为mid - inBegin;
TreeNode * left = createTree(preOrder, preBegin + , preBegin + len, inOrder, inBegin, mid - );
TreeNode * right = createTree(preOrder, preBegin + len + , preEnd, inOrder, mid + , inEnd);
TreeNode * root = new TreeNode(rootVal);
root->left = left;
root->right = right;
return root;
}
};
java版本的代码如下所示,方法上与上面的没什么区别:
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return createTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
} public TreeNode createTree(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd){
if(preBegin > preEnd)
return null;
int rootVal = preorder[preBegin];
int i = inBegin;
for(; i <= inEnd; ++i){
if(inorder[i] == rootVal)
break;
}
int leftLen = i - inBegin;
TreeNode root = new TreeNode(rootVal);
root.left = createTree(preorder, preBegin + 1, preBegin + leftLen ,inorder, inBegin, i - 1); //这里的边界条件应该注意
root.right = createTree(preorder, preBegin + 1 + leftLen, preEnd, inorder, i + 1, inEnd);
return root;
}
}
LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)的更多相关文章
- 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 ...
- 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 t ...
- [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 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- (二叉树 递归) 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 ----- java
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】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(根据前序中序构建二叉树)
根据前序中序构建二叉树. 1 / \ 2 3 / \ / \ 4 5 6 7对于上图的树来说, index: 0 1 2 3 4 5 6 先序遍历为: 6 3 7为了清晰表示,我给节点上了颜色,红色是 ...
随机推荐
- Java基础—String总结
特点 String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. String实 ...
- HDU 3199 Hamming Problem
Hamming Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 字典树 trie树 学习
一字典树 字典树,又称单词查找树,Trie树,是一种树形结构,哈希表的一个变种 二.性质 根节点不包含字符,除根节点以外的每一个节点都只包含一个字符: 从根节点到某一节点,路径上经过的字符串连接起 ...
- 8位单片机可用的 mktime localtime函数
8位单片机可用的 mktime localtime函数及源码 最近在做一个8位单片机项目,其中用到了时间戳转换函数,这个在32位机上一个库函数就解决了问题,没想到在8位单片机中没有对应库(time. ...
- 【转载】iptables、tc和ip命令
2.3 CommandListener中的命令 CL一共定义了11个命令,这些命令充分反映了Netd在Android系统中网络管理和控制方面的职责.本节首先介绍Linux系统中常用的三个网络管理工具, ...
- Python编程-网络编程进阶(IO复用、Socketserver)
一.认证客户端的链接合法性 如果你想在分布式系统中实现一个简单的客户端链接认证功能,又不像SSL那么复杂,那么利用hmac+加盐的方式来实现. 服务端 from socket import * imp ...
- jQuery计算器插件
在线演示 本地下载
- INSPIRED启示录 读书笔记 - 第34章 恐惧、贪婪、欲望
消费者购买产品大多源于情感需求 企业级消费者出于恐惧和贪婪购买产品:如果不买这款产品,竞争对手会超过我,黑客会攻破我的防火墙,客户将弃我而去:如果买了,会赚得更多,省得更多 大众消费者购买产品的原因更 ...
- relativePath
比如: com.tenace tenace 2.0.1 ../pom.xml //刚开始无此句 com.spider engine 2.6.0-SNAPSHOT tenace作为pom项目已经发布至r ...
- poj 3126 Bfs
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14539 Accepted: 8196 Descr ...