【LeetCode笔记】Construct Binary Tree from Inorder and Postorder Traversal 中序、后序遍历求二叉树
来源:互联网 发布:隔热断桥铝合金 知乎 编辑:程序博客网 时间:2024/06/09 14:27
思路:
1.和上一题很像啦~~~后序遍历的最后一个点可以把中序遍历分割成前后两部分,前面的部分就是分割点的左子树,后面的部分就是分割点的右子树。以及类推可递归。
具体细节和分析在上一题中都有,就不展开说了~
/** * 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* build( vector<int>& inorder, vector<int>& postorder, int i_s, int i_e, int p_s, int p_e){ if (i_s>i_e||p_s>p_e) return NULL; int com = postorder[p_e]; int i = i_s; for(;i<i_e;i++){ if (com==inorder[i]) break; } TreeNode* tree = new TreeNode(postorder[p_e]); tree->left = build(inorder,postorder,i_s,i-1,p_s,p_s+i-i_s-1); tree->right = build(inorder,postorder,i+1,i_e,p_s+i-i_s,p_e-1); return tree; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { return build(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1); }};这次有点小开心~~也可能是因为这几道连续的题目套路都差不多,所以感觉自己刷题越来越快啦~~
0 0
- 【LeetCode笔记】Construct Binary Tree from Inorder and Postorder Traversal 中序、后序遍历求二叉树
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- [LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- lintcode construct-binary-tree-from-inorder-and-postorder-traversal 中序遍历和后序遍历树构造二叉树
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- 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(根据中序遍历和后序遍历构造二叉树)
- Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序重建二叉树@LeetCode
- 106.leetcode.Construct Binary Tree from Inorder and Postorder Traversal(medium)[中序、后序构造二叉树]
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- Construct Binary Tree from Inorder and Postorder Traversal 利用中序遍历和后序遍历构造树
- LeetCode(Construct Binary Tree from Inorder and Postorder Traversal)根据二叉树的中序和后续构造二叉树
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal 中序后序遍历重组二叉树
- 72.Construct Binary Tree from Inorder and Postorder Traversal-中序遍历和后序遍历树构造二叉树(中等题)
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal (利用中序遍历和后续遍历确定一颗二叉树)
- 计算机网络和因特网--分组交换网中的时延,丢包和吞吐量
- 找中位数
- GDOI模拟总结4.11-4.13(实时更新)
- 腾讯面试(二)
- 拉格朗日乘子法
- 【LeetCode笔记】Construct Binary Tree from Inorder and Postorder Traversal 中序、后序遍历求二叉树
- GDOI2017模拟04.11总结
- 用Docker搭建WordPress博客
- centos防火墙操作命令
- 面试机试之序列前n项和
- select下拉框&radio单选按钮数据的回显
- Faster R-cnn 或Caffe hdf5库目录找不到解决办法
- 文章标题
- 【LeetCode笔记】Binary Tree Level Order Traversal II 二叉树按层遍历,反向输出