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(从前序以及中序遍历结果中构造二叉树)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  5. (二叉树 递归) 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 【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 ...

  10. 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为了清晰表示,我给节点上了颜色,红色是 ...

随机推荐

  1. 20170413 F110学习

                  F110 学习: Tcode: F110  自动付款业务, FBZP   维护收付程序设置 FBL1N   供应商行项目 XK03   显示供应商(银行信息维护) F110 ...

  2. LeetCode-11-7

    1.Reverse String Write a function that takes a string as input and returns the string reversed. Exam ...

  3. JavaWeb:JSP标准标签库

    JavaWeb:JSP标准标签库 说明 什么是JSTL? JSP标准标签库(JavaServer Pages Standard Tag Library,JSTL)是一个定制的标签库的集合,用来解决像遍 ...

  4. 小程序 height100% Android ios上的不同表现

    Android还是按原图显示 ios,会完全覆盖

  5. PHP实现生成唯一编号(36进制的不重复编号)

    当我们要将一个庞大的数据进行编号时,而编号有位数限制,比如5位的车牌号.10位的某证件号码.订单流水号.短网址等等,我们可以使用36进制计算出符合位数的不重复的编号. 我们将0-Z(012345678 ...

  6. javascript重置(base层)(。。。。不完整)

    1.nextSibling浏览器兼容问题 <ul> <li id="item1"></li> <li id="item2&quo ...

  7. [转]AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)

    AOP那些学术概念—通知.增强处理连接点(JoinPoint)切面(Aspect) 1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充 ...

  8. p2p网络中的NAT穿透技术----常见NAT穿越解决方案

    转:http://blog.csdn.net/cllzw/article/details/46438257 常见NA丁穿越解决方案 NAT技术在缓解IPv4地址紧缺问题.构建防火墙.保证网络安全等方面 ...

  9. 以太坊钱包Geth使用命令

    一.启动以太坊钱包Geth 打开一个控制台,执行同步区块命令 #同步测试链geth --fast --cache=512 --rpc --rpcapi personal,db,eth,net,web3 ...

  10. 主攻ASP.NET MVC4.0之重生:Jquery Mobile 按钮+对话框使用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...