Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
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.
递归构建。
思路就是: preorder可以定位到根结点,inorder可以定位左右子树的取值范围。
1. 由preorder得到根结点;把preorder第一个点删掉;
2. 先建左子树;再建右子树;
通过一个区间来表示左右子树的取值范围。因为inorder左右子树的范围都是连续的。中间就是root。
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return recursive(preorder, inorder, , inorder.size() - );
}
TreeNode* recursive(vector<int> &preorder, vector<int> &inorder, int s, int e) {
if (s > e) return NULL;
if (preorder.empty()) return NULL;
TreeNode *root = new TreeNode(preorder.front());
preorder.erase(preorder.begin());
int i = s;
for (; i <= e && inorder[i] != root->val; ++i);
root->left = recursive(preorder, inorder, s, i - );
root->right = recursive(preorder, inorder, i + , e);
}
};
Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
和上面类似。有两点不同。
1. postorder,最后一个元素是根结点。
2. 先构建右子树,再构建左子树。
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return recursive(postorder, inorder, , inorder.size() - );
}
TreeNode* recursive(vector<int> &postorder, vector<int> &inorder, int s, int e) {
if (s > e) return NULL;
if (postorder.empty()) return NULL;
TreeNode *root = new TreeNode(postorder.back());
postorder.pop_back();
int i = s;
for (; i <= e && inorder[i] != root->val; ++i);
root->right = recursive(postorder, inorder, i + , e);
root->left = recursive(postorder, inorder, s, i - );
}
};
Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal的更多相关文章
- 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 ...
- 105. Construct Binary Tree from Inorder and preorder Traversal
/* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- [LeetCode] Construct Binary Tree from Inorder and Pretorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume th ...
随机推荐
- Android stadio
Android stadio 最近遇到大问题,就是主功能行.但是让它做库工程,他就不管用. 但是在eclipse里面就可以.
- netcfg.exe
netcfg.exe 编辑 本词条缺少信息栏.名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 目录 1 简介 2 可能出现问题 简介编辑 netcfg.exe是Kaspersky的 ...
- NodeJs初学者经典入门解析
Node.js 是一个基于谷歌浏览器JavaScript执行环境建立的一个平台,让JavaScript可以脱离客户端浏览器运行,让 JavaScript具有服务器语言的能力.我们可以使用NodeJs方 ...
- [转]jQuery DOM Ready
一直以来,各种JS最佳实践都会告诉我们,将JS放在HTML的最后,即</body>之前,理由就是:JS会阻塞下载,而且,在JS中很有可能有对DOM的操作,放在HTML的最后,可以尽可能的保 ...
- ZigBee学习四 无线+UART通信
ZigBee学习四 无线+UART通信 1) 协调器编程 修改coordinator.c文件 byte GenericApp_TransID; // This is the unique messag ...
- ABC128F Frog Jump
题目链接 题目大意 给定一个长为 $n$ 的数组 $s$,下标从 $0$ 开始.$ 3 \le n \le 10^5$,$-10^9 \le s_i \le 10^9$,$s_0 = s_{n - 1 ...
- crontab中执行java程序的脚本
测试场景说明(操作系统:centos7): 有一个bash脚本,脚本内容是执行某个java程序,该脚本为 /data/project1/start.sh crontab -e,添加了以下任务: * * ...
- [BZOJ]5018: [Snoi2017]英雄联盟 DP
[Snoi2017]英雄联盟 Time Limit: 15 Sec Memory Limit: 512 MBSubmit: 270 Solved: 139[Submit][Status][Disc ...
- git 以及 工作区 版本库 暂存区
https://www.jianshu.com/p/a308acded2ce 这个博客介绍的比较简单 https://blog.csdn.net/qq_31828515/arti ...
- 【12】vue-router 之路由重定向
看之前的项目,突然发现一个不算bug的bug,之前也是一直没有想到,现在发现之后越来越觉得有必要改掉, 项目用的是vue做的,自然切换用的就是路由,一级路由包括:首页.记录和个人中心,二级路由是在记录 ...