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. Ubuntu14.04下Nginx反向代理Odoo域名

    安装nginx sudo apt-get install -y nginx 修改配置文件 vi /etc/nginx/nginx.conf #注释掉下面这行代码 #include /etc/nginx ...

  2. qemu-img 的使用

    qemu-img是QEMU的磁盘管理工具,在qemu-kvm源码编译后就会默认编译好qemu-img这个二进制文件.qemu-img也是QEMU/KVM使用过程中一个比较重要的工具,本节对其用法和实践 ...

  3. vim常用快捷键记录

    yy复制一行 2yy复制2行 同理 3yy复制3行 p粘贴复制 dd删除一行 ctrl+f 翻页 ctrl+b 上翻 shift+a 跳到行尾进入insert模式 shift+i 跳到行首进入inse ...

  4. EXCEL 从网页复制的内容 单/多选框 在EXCEL删不掉 及 2007添加开发工具选项卡

    从网页复制到Excel中的单选.多选框等,有时候删除时怎么都删不掉,很是恶心.这时候需要使用“开发工具”来删除.它是设计模式下的一种组件或者说控件. Excel 2007 的可以用下图方式按delet ...

  5. MySQL数据库(1)_MySQL数据库介绍与安装

    一.数据库相关概念的简介 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展 ...

  6. gzframework开发记录

    修改窗体权限: 重写方法 修改操作按钮名称 全部自定义增加操作按钮: 插入控件顺序: InsertAfterButton插入到指定控件后面 InsertBeforeButton插入到指定控件前面 公共 ...

  7. java经典30笔试题

    1. 下面哪些是Thread类的方法() A start()       B run()       C exit()       D getPriority() 答案:ABD 解析:看Java AP ...

  8. KVC和KVO的理解(底层实现原理)

    1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的 ...

  9. QT应用程序设置图标

    一.纯Qt 1.下载图标:app.ico 2.新建记事本,输入:IDI_ICON1 ICON DISCARDABLE"app.ico":改变名字为jude.rc 3.将两个文件放在 ...

  10. Kubernetes busybox nslookup问题

    使用最新版本的busybox会出现nslookup提示无法解析的问题: Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find k ...