leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal
这道题是为数不多的感觉在读本科的时候见过的问题。
人工构造的过程是如何呢。兴许遍历最后一个节点一定是整棵树的根节点。从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右側的递归构造右子树。元素本身分配空间,作为根节点。
于set和map容器不同的是。vector容器不含find的成员函数。应该用stl的库函数,好在返回的也是迭代器,而vector的迭代器之间是能够做减法的。偏移量非常方便的得到。
TreeNode *buildRec(vector<int> &inorder, int si, vector<int> &postorder, int so, int len){if(len <= 0) return NULL;TreeNode *root = new TreeNode(postorder[so]);int index = find(inorder.begin(), inorder.end(), postorder[so]) - inorder.begin();int newlen = index - si;root->left = buildRec(inorder, si, postorder, so-len+newlen, newlen);root->right = buildRec(inorder, index+1, postorder, so-1, len-newlen-1);return root;
}class Solution {
public:TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {return buildRec(inorder, 0, postorder, inorder.size()-1, inorder.size());}
};
转载于:https://www.cnblogs.com/blfshiye/p/5135053.html
leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal相关推荐
- 【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 106. Construct Binary Tree from Inorder and Postorder Traversal
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...
- 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 刷题之路 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 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 ...
- 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 ...
- 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 assu ...
- Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
最新文章
- python中文字符画、编写程序合理选取中文字符构造_编写程序,合理选取中文字符构造字符表,生成中文字符画。...
- 面试官:哪些场景会产生OOM?怎么解决?
- Java Web系列:Spring MVC基础
- python_面向对象进阶之slots
- Linux的cmake3的安装 cmake3编译安装成功了的 yum对于cmake3表示成功但实际没成功
- 未能加载包studio package_Xrepo:一个现代化的跨平台 C/C++ 包管理器
- python 删除重复字符_Google面试问题指南:使用Python删除重复出现的字符
- 计算机的微程序存放在dram,计算机组成与结构
- linux 自动获取ip和dns,linux 配置IP和DNS
- java递归生成树结构_突破CRUD | 万能树Java工具类封装(源码)
- 对zebra的一点思考 --- 002
- 如何选择VC界面库产品?(二)– DSkinLite界面库介绍
- 学习Spring之前要先学习什么?
- 韩立刚老师《计算机网络》笔记3
- 基统计的方法对进行多因子的探索性数据分析
- POI word 内容提取 Strict OOXML isn‘t currently supported, please see bug #57699
- 禁止查看网页源文件的代码
- LibJpeg的安装与修复颜色错误图像错位保姆级教程
- 浏览器输入url后经历的过程(详细)
- git命令和遇见得 warning:
热门文章
- 部署IPV6有什么好处?
- vs中imshow函数报错_Win7下VS2010中配置Opencv2.4.4的方法(32位和64位都有效)(亲测成功)...
- python结束线程池正在运行的线程_python之线程与线程池
- STP文件服务器,综合监控stp服务器
- tmp ubuntu 自动删除吗_Linux tmp目录自动清理总结
- vue是什么_什么是VUE?vue有什么作用?
- 您不是订单管理的定向开发者_Web Summit 2020大会:华为在欧洲发布HMS Connect,持续助力合作伙伴与开发者的创新增长...
- 图像“位操作”有什么用?
- 分类模型的评估方法-正确率(Accuracy)
- Python几个简单好用的基础功能