Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3 / \ 9 20 / \ 15 7
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
if(postorder.size() == 0)
return NULL;
if(postorder.size() == 1)
return new TreeNode(postorder[0]);
int iroot = postorder[postorder.size() - 1];
int ipos = 0;
for(int i = 0; i < inorder.size(); i++)
{
if(inorder[i] == iroot)
{
ipos = i;
break;
}
}
TreeNode *root = new TreeNode(iroot);
vector<int> v1(postorder.begin(), postorder.begin() + ipos);
vector<int> v2(inorder.begin(), inorder.begin() + ipos);
vector<int> v3(postorder.begin() + ipos, postorder.end() - 1);
vector<int> v4(inorder.begin() + ipos + 1, inorder.end());
root ->left = buildTree(v2, v1);
root ->right = buildTree(v4, v3);
return root;
}
};
Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树的更多相关文章
- LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal
题目 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9, ...
- 106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树
给定一棵树的中序遍历与后序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出中序遍历 = [9,3,15,20,7]后序遍历 = [9,15,7,20,3]返回如下的二叉树: ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 ...
- 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 ...
- 【题解二连发】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 ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
随机推荐
- C# 封装首页、上一页、下一月、尾页处理器
public void BtnPageClickEvent(object sender,string focusForeground,string lostFocusForeground) { But ...
- 13-2-return
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 试做Chrome插件——whatweb的chrome插件(从老博客转)
引子 最近一个月每天早上在学Javascript,刚学完基础语法和一点点jQuery,今天忍不住写个Chrome玩玩看看自己对JavaScript的掌握怎么样了. 目标 考虑了一下,打算做个小东西,但 ...
- Atcoder arc096
C:Half and Half 几个if语句贪心算一算就好了 #include<cstdio> #include<algorithm> using namespace std; ...
- 廖雪峰Java15JDBC编程-2SQL入门-1SQL介绍
1.SQL:结构化查询语言 Structured Query Language 针对关系数据库设计 各种数据库基本一致 允许用户通过SQL查询数据而不关心数据库底层存储结构 1.1 SQL使用: 可以 ...
- JedisCluster API 整理
windows版redis启动服务器命令:redis-server redis.windows.conf 图表来自菜鸟教程: 列表的操作命令 序号 命令及描述 1 BLPOP key1 [key2 ] ...
- String相加解析
Java代码 package com.utils.test; public class TestObject { public static void main(String[] args) { St ...
- KMP算法简明法则
KMP算法也算是相当经典,但是对于初学者来说确实有点绕,大学时候弄明白过后来几年不看又忘记了,然后再弄明白过了两年又忘记了,好在之前理解到了关键点,看了一遍马上又能理解上来.关于这个算法的详解网上文章 ...
- 「题解」NOIP模拟测试题解乱写I(29-31)
NOIP模拟29(B) T1爬山 简单题,赛时找到了$O(1)$查询的规律于是切了. 从倍增LCA那里借鉴了一点东西:先将a.b抬到同一高度,然后再一起往上爬.所用的步数$×2$就是了. 抬升到同一高 ...
- jquery ajax 放在重复点击事件beforeSend方法
防止重复数据在实际项目开发中,提交表单时常常由于网络或者其原因,用户点击提交按钮误认为自己没有操作成功,进而会重复提交按钮操作次数,如果页面前端代码没有做一些相应的处理,通常会导致多条同样的数据插入数 ...