leetcode--Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:日本对外贸易数据2015 编辑:程序博客网 时间:2024/06/06 03:18
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
分类:二叉树
题意:由前序遍历,中序遍历生成二叉树
解法1:递归。将前序遍历和中序遍历分成两部分。对于前序遍历而言,数组第一个元素为根节点。在中序遍历中找到这个根节点,其左右两边则分别是左右子树的中序遍历。
计算左子树长度len(根据根节点index求出),然后对应于前序遍历,从根节点(数组第一个节点)开始,len长度之内,同样是左子树的前序遍历,剩下的是右子树的前序遍历。
这样我们又得到两个中序遍历和前序遍历,递归求解即可。
原文链接http://blog.csdn.net/crazy__chen/article/details/46484415
阅读全文
0 0
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode-Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode - Construct Binary Tree from Preorder and Inorder Traversal
- [leetCode] Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- hbase中Bytes类的使用
- 解决JDK的安装路径中有空格的情况
- Jedis Object has already been returned to this pool or is invalid
- Caffe(CPU)-Ubuntu16.04下安装教程(未完成)
- php mysql数据库备份
- leetcode--Construct Binary Tree from Preorder and Inorder Traversal
- 蓝桥杯-兰顿蚂蚁(Java)
- poj 2777 Count Color(线段树区间更新+技巧)
- lintcode刷题——快速幂
- javascript/js 原生动态引入外部css文件及动态插入css代码片段
- FileZilla上传文件到外网FTP服务器响应:550 Permission denied.
- Scala实战—连接oracle及mysql数据库
- overlay 如何实现跨主机通信?- 每天5分钟玩转 Docker 容器技术(52)
- Android Translucent System Bar 开发详解-实现沉浸式通知栏(通知栏与导航栏颜色相同)