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 duplicates do not exist in the tree.
此题目有两种解决思路:
1)递归解决(比较好想)按照手动模拟的思路即可
2)非递归解决,用stack模拟递归
class Solution {
public:
TreeNode *buildTree(vector<int>& preorder, int pre_left,int pre_right,
vector<int>& inorder, int in_left, int in_right){
if(pre_left > pre_right || in_left > in_right) return NULL;
TreeNode *root = new TreeNode(preorder[pre_left]);
int index = in_left;
for( ; index <= in_right; ++ index ) if(inorder[index] == preorder[pre_left]) break;
int left_cnt = index-in_left;
root->left = buildTree(preorder,pre_left+,pre_left+left_cnt,inorder,in_left,index-);
root->right = buildTree(preorder,pre_left+left_cnt+,pre_right, inorder, index+,in_right);
return root;
} TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
if(preorder.size() == ) return NULL;
else return buildTree(preorder,,preorder.size()-, inorder,,inorder.size()-);
}
};
递归解决
Leetcode Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- LeetCode: 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 ...
- LeetCode——Construct Binary Tree from Preorder and Inorder Traversal
Question Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as ...
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal
总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...
- 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 ...
- 【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 ...
- 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 ...
- 【题解二连发】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 ...
随机推荐
- python中统计列表各个元素的个数
- sql server 的cpu使用率过高的分析
有哪些SQL语句会导致CPU过高? 1.编译和重编译 编译是 Sql Server 为指令生成执行计划的过程.Sql Server 要分析指令要做的事情,分析它所要访问的表格结构,也就是生成执行计划的 ...
- 数据库支持emoji表情
从MySQL5.5.3开始,MySQL 支持一种utf8mb4的字符集,这个字符集能够支持4字节的UTF8编码的字符.utf8mb4字符集能够完美地兼容utf8字符串.在数据存储方面,当一个普通中文字 ...
- tomcat7源码编译过程以及问题解决
http://blog.csdn.net/kaoshangqinghua/article/details/40022315
- linux中创建gpio节点
转自:http://blog.chinaunix.net/uid-29165999-id-4296162.html #define GPIO_MAJOR 230 // major device NO. ...
- Spring.Net学习之简单的知识点(一)
1.Spring.Net是一个开源的应用程序框架,可以简化开发主要功能(1)实现控制反转(IOC/DI),也就是不要直接new,依赖于接口(2)面向切面编程(AOP),就是向程序中利用委托注册事件简单 ...
- Android性能优化系列 + Android官方培训课程中文版
Android性能优化典范 - 第6季 http://hukai.me/android-performance-patterns-season-6/ Android性能优化典范 - 第5季 htt ...
- Pyqt phonon的使用
本文是用Pyqt实现了下网上一个Qt版大牛关于phonon的介绍 Qt phonon地址:http://wenku.baidu.com/link?url=nH_dZ8lZbXHy8N5__8jAWLX ...
- Solr入门之(4)配置文件solr.xml
<?xml version="1.0" encoding="UTF-8" ?> <!-- This is an example of a si ...
- Oracle 10g Block Change Tracking特性
Using Block Change Tracking to Improve Incremental Backup Performance 使用块改变跟踪改善增量备份的性能 The block cha ...