leetcode 刷题之路 64 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.
给出二叉树的中序遍历和后序遍历结果,恢复出二叉树。
后序遍历序列的最后一个元素值是二叉树的根节点的值。查找该元素在中序遍历序列中的位置mid,依据中序遍历和后序遍历性质。有:
位置mid曾经的序列部分为二叉树根节点左子树中序遍历的结果,得出该序列的长度n,则后序遍历序列前n个元素为二叉树根节点左子树后序遍历的结果。由这两个中序遍历和后序遍历子序列恢复出左子树。
位置mid以后的序列部分为二叉树根节点右子树中序遍历的结果,得出该序列的长度m,则后序遍历序列(除去最后一个元素)后m个元素为二叉树根节点右子树后序遍历的结果,由这两个中序遍历和后序遍历子序列恢复出左子树;
以上描写叙述中递归地引用了由中序遍历和后序遍历恢复子树的部分,因此程序也採用递归实现。
AC code:
/*** Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/TreeNode *helper(vector<int> &inorder, int b1, int e1, vector<int> &postorder, int b2, int e2){if (b1>e1)return NULL;int mid;for (int i = b1; i <= e1; i++)if (inorder[i] == postorder[e2]){mid = i;break;}TreeNode* root = new TreeNode(inorder[mid]);root->left = helper(inorder, b1, mid - 1, postorder, b2, b2 + mid - b1-1);root->right = helper(inorder, mid + 1, e1, postorder, b2 + mid-b1, e2 - 1);return root;}class Solution {public:TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder){return helper(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);}};
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5207473.html,如需转载请自行联系原作者
leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal相关推荐
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...
- 【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 -day23 Construct Binary Tree from Inorder and Postorder Traversal Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- 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 ...
- 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 - 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 tha ...
- Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- 【Construct Binary Tree from Inorder and Postorder Traversal】cpp
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal | 106. 从中序后序遍历序列构造二叉树(Java)
题目 https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题解 待优化: ...
最新文章
- Nat Commun:宏基因组学提示曙古菌门的代谢和进化(中大李文均组)
- cmake使用示例与整理总结_QTVLC的博客-CSDN博客_cmake使用示例与整理 施公队演示时用的blog B zhan
- 嵌入式环境:挂载开发板根NFS文件系统失败
- 安装android到u盘安装程序,在u盘内安装android教程
- WIN 10 安装 Hadoop 2.7.7 + Spark 2.4.7 记录
- 测试局域网路的MTU最大值
- [转载] Python_range()_逆序
- POJ 1192 最优连通子集(树形DP)
- arduino atmega328P MCP4725 proteus 仿真 程序
- 代理服务器和IP加速器之间有什么关系?
- 鼠标滚轮无法滚动处理、有滚动条但鼠标中间的滚轮滚动时页面不随之滚动
- MPLS(Multi-Protocol Label Switching)——多协议标签交换
- 火狐插件restclient发送post请求
- 2021年中国医药工业经济运行现状及行业发展建议:主营业务收入、利润总额整体递增,建议加大监管,引导产业良性发展[图]
- QT入门学习之软件程序开发初体验
- 前端进击的巨人(六):知否知否,须知this
- 启动不了argis的license manager
- 【举例说明】Word中如何自动生成目录以及设置格式
- 测试开发工程师面试总结(一)——Java基础篇
- python语句中print(0xa+0xb)的输出结果是_【单选题】Python语句print(0xA+0xB)的输出结果是( )...
热门文章
- Django 开发中的最佳实践之一
- 调试器定位变量的原理
- 通过telnet命令查看memcache运行状态
- C#笔记24:善用Visual Studio
- 服务器系统ghost版 raid,服务器在raid5下做系统ghost备份.docx
- swift4.0 try 的强大
- 操作系统内存管理_操作系统6内存管理基础
- Xamarin.iOS真机测试报错
- 指定的网络文件夹目前是以其他用户名和密码进行映射的_使用 GitLab CI 与 Argo CD 进行 GitOps 实践
- 简述python程序结构_python架构的概念讲解