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.
给出一棵二叉树的中根遍历和后根遍历,求这棵树。
基本思路:我们有in和post数组长度都为7,那么我们可以知道post[6]是根。找到post[6]对应in中的位置,假设是in[3]。那么可以得出in[0]~in[2]为左子树,in[4]~in[6]为右子树。同理继续向下递归。
/*** 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 solve(0, postorder.size()-1, 0, inorder.size()-1, inorder, postorder);}TreeNode* solve(int post, int poed, int inst, int ined, vector<int>& inorder, vector<int>& postorder) {if (inst > ined || post > poed) return NULL;TreeNode* cur = new TreeNode(postorder[poed]);int index = 0;for (int i = inst; i <= ined; ++i) {if (inorder[i] != cur->val) continue;index = i;break;}cur->left = solve(post, poed-(ined-index+1), inst, index-1, inorder, postorder);cur->right = solve(post+(index-inst), poed-1, index+1, ined, inorder, postorder);return cur;}
};
LeetCode - 106 - 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
题目 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/ 题解 待优化: ...
- 【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 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- 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 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 ...
最新文章
- gitlab提交合并_notification - 如何把gitlab项目中的提交、PUSH、合并请求的通知发送到邮箱...
- 谈谈JVM内部锁升级过程
- linux chromebook arm,第一款可拆卸 ARM Chromebook 可能是 KODAMA
- java mongodb 多文档_如何通过Java在MongoDB中一次性插入多个文档
- vsftpd的基于pam_mysql的虚拟用户机制实现数据传输
- JSP页面添加播放视频功能
- MSMQ 和 MQTT
- 点云算法(深度学习)
- mysql左连接查询举例_mysql左右连接查询(有示例图)
- 博弈论:帕累托最优与纳什均衡的联系和区别
- java cxf encoded,java:JAXWS 2.0不支持Rpc / encoded wsdls
- 2020美容师(初级)模拟考试及美容师(初级)复审模拟考试
- 以昂扬的斗志,书写青春的热血
- 3d打印在影视领域应用
- seq2seq 解释
- 沪漂程序媛妹子的一天...
- 迟到的年终总结,2012-2014
- 员工股权激励_激励技术员工的5种方法
- 分分钟拯救监控知识体系
- Java工程师需要掌握哪些知识呢?
热门文章
- 小米android11适配计划,MIUI:小米10因适配Android 11,开发版暂停更新!
- Hadoop中小文件过多的问题
- DSMM是什么?一篇内容让你快速了解
- 建構收益率曲線 Yield Curve (1) 工作日調整 Business Day Convention - 用 Python 和 QuantLib
- GWAS(1)——文件格式及linux上的转换
- 计算机显卡设置方法,电脑小技巧:显卡巧设置,让你的电脑告别卡顿
- 作为PM,PMP证书重要吗?
- 新标准日本语初级 第一课
- 【对IIS搭建网站和借助花生壳内网穿透的尝试】
- 【控制】滑模控制,滑模面的选择