(二叉树 递归) leetcode 106. 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.
For example, given
- inorder = [9,3,15,20,7]
- postorder = [9,15,7,20,3]
Return the following binary tree:
- 3
- / \
- 9 20
- / \
- 15 7
- -------------------------------------------------------------------------------------
就是从中序遍历和后序遍历构建二叉树。可以用递归方式。注意递归的终止条件。
和leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal几乎一样。- 参考博客:http://www.cnblogs.com/grandyang/p/4296193.html
- C++代码:
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
- return build(inorder,,inorder.size()-,postorder,,postorder.size() - );
- }
- TreeNode *build(vector<int> &inorder,int ileft,int iright,vector<int> &postorder,int pleft,int pright){
- if(ileft > iright ||pleft > pright) return NULL; //终止条件,就是当序列的长度为0时,递归终止。
- int i = ;
- TreeNode *cur = new TreeNode(postorder[pright]);
- for(i = ileft; i < inorder.size(); i++){
- if(inorder[i] == cur->val)
- break;
- }
- cur->left = build(inorder,ileft,i-,postorder,pleft,pleft + i - ileft - );
- cur->right = build(inorder,i + ,iright,postorder,pleft + i - ileft,pright - );
- return cur;
- }
- };
还有一个方法,就是建立几个数组,保存分割后的数组。不过时间会很长。
C++代码:
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
- return build(inorder,postorder);
- }
- TreeNode *build(vector<int> &inorder,vector<int> &postorder){
- if(inorder.size() == || postorder.size() == ) //递归条件,当然也可以加上if(inorder.size() == 1 || postorder.size() == 1)return cur;这个递归条件。
- return NULL;
- int rootval = postorder.back();
- TreeNode *cur = new TreeNode(rootval);
- int i = ;
- for(i = ; i < inorder.size(); i++){
- if(inorder[i] == rootval) break;
- }
- vector<int> inleft,inright;
- vector<int> poleft,poright;
- for(int j = ; j < i; j++){
- inleft.push_back(inorder[j]);
- poleft.push_back(postorder[j]);
- }
- for(int j = i + ; j < inorder.size(); j++){
- inright.push_back(inorder[j]);
- }
- for(int j = i; j < postorder.size() - ; j++){
- poright.push_back(postorder[j]);
- }
- cur->left = build(inleft,poleft);
- cur->right = build(inright,poright);
- return cur;
- }
- };
这两个方法从算法上看是一样的,只是代码的实现不同而已。
(二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- [LeetCode] 106. 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 106. 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 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- C#解leetcode 106. 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 889. Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)
原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
随机推荐
- ReSharper导致Visual Studio缓慢?
问题排查 我们会竭尽所能的ReSharper的性能方面,但是也有一些已知和未知的情况下,ReSharper的可以减缓的Visual Studio. 这里有一些关键点进行故障排除和修复ReSharper ...
- Cs231n-assignment 1作业笔记
KNN assignment1 KNN讲解参见: https://blog.csdn.net/u014485485/article/details/79433514?utm_source=blogxg ...
- python+selenium 输出2种样式的测试报告
第一种: 1.通过 HTMLTestRunner 模块输出报告 2.下载连接 http://tungwaiyip.info/software/HTMLTestRunner.html 3.将下载好的文件 ...
- Java操作Excel(使用POI)
背景说明 以前写过使用 JXL 操作Excel的例子,但JXL对于Excel 2007版本以后的文件(即扩展名为 .xlsx)无法读取,也找不到可以支持的包.所以,有时不得不用 POI 来操作Exce ...
- C# 里面swith的或者
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 【转】在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- windows下搭建nginx+php+laravel开发环境(转)
1.前言 windows下大多我们都是下载使用集成环境,但是本地已经存在一个集成环境,但不适合项目的需求.因此准备再自己搭建一个环境. 2.准备 工具: 1) 下载 nginx1.14.0(版本根据自 ...
- JS正则四个反斜杠的含义
我们首先来看如下代码,在浏览器中输出的是什么? // 在浏览器中输出的 console.log('\\'); // 输出 \ console.log('\\\\'); // 输出 \\ 一:js正则直 ...
- 一文搞懂Raft算法
raft是工程上使用较为广泛的强一致性.去中心化.高可用的分布式协议.在这里强调了是在工程上,因为在学术理论界,最耀眼的还是大名鼎鼎的Paxos.但Paxos是:少数真正理解的人觉得简单,尚未理解 ...
- 即将发布的 ASP.NET Core 2.2 会有哪些新玩意儿?
今年 6 月份的时候时候 .NET 团队就在 GitHub 公布了 ASP.NET Core 2.2 版本的 Roadmap(文末有链接),而前两天 ASP.NET Core 2.2 预览版 2 已经 ...