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相关推荐

  1. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal

    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...

  2. 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 ...

  3. leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal | 106. 从中序后序遍历序列构造二叉树(Java)

    题目 https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题解 待优化: ...

  4. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  5. 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 ...

  6. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  7. 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 ...

  8. 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 ...

  9. Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

最新文章

  1. gitlab提交合并_notification - 如何把gitlab项目中的提交、PUSH、合并请求的通知发送到邮箱...
  2. 谈谈JVM内部锁升级过程
  3. linux chromebook arm,第一款可拆卸 ARM Chromebook 可能是 KODAMA
  4. java mongodb 多文档_如何通过Java在MongoDB中一次性插入多个文档
  5. vsftpd的基于pam_mysql的虚拟用户机制实现数据传输
  6. JSP页面添加播放视频功能
  7. MSMQ 和 MQTT
  8. 点云算法(深度学习)
  9. mysql左连接查询举例_mysql左右连接查询(有示例图)
  10. 博弈论:帕累托最优与纳什均衡的联系和区别
  11. java cxf encoded,java:JAXWS 2.0不支持Rpc / encoded wsdls
  12. 2020美容师(初级)模拟考试及美容师(初级)复审模拟考试
  13. 以昂扬的斗志,书写青春的热血
  14. 3d打印在影视领域应用
  15. seq2seq 解释
  16. 沪漂程序媛妹子的一天...
  17. 迟到的年终总结,2012-2014
  18. 员工股权激励_激励技术员工的5种方法
  19. 分分钟拯救监控知识体系
  20. Java工程师需要掌握哪些知识呢?

热门文章

  1. 小米android11适配计划,MIUI:小米10因适配Android 11,开发版暂停更新!
  2. Hadoop中小文件过多的问题
  3. DSMM是什么?一篇内容让你快速了解
  4. 建構收益率曲線 Yield Curve (1) 工作日調整 Business Day Convention - 用 Python 和 QuantLib
  5. GWAS(1)——文件格式及linux上的转换
  6. 计算机显卡设置方法,电脑小技巧:显卡巧设置,让你的电脑告别卡顿
  7. 作为PM,PMP证书重要吗?
  8. 新标准日本语初级 第一课
  9. 【对IIS搭建网站和借助花生壳内网穿透的尝试】
  10. 【控制】滑模控制,滑模面的选择