106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:蓝牙分享软件 编辑:程序博客网 时间:2024/06/07 19:19
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题意:根据中序序列和后序序列,构建出一颗二叉树。
思路:先找根,再找左树的范围,再找右树的范围,分别构建左树右树。
/** * 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) {if (inorder.empty())return NULL;return build(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);}private:TreeNode* build(vector<int>& inorder, int i1, int i2, vector<int>& postorder, int p1, int p2){if (i1 > i2 || p1 > p2)return NULL;TreeNode* root = new TreeNode(postorder[p2]);vector<int>::iterator it = find(inorder.begin() + i1, inorder.begin() + i2, postorder[p2]);int imid = it - inorder.begin();int leftSize = imid - i1;root->left = build(inorder, i1, imid-1, postorder, p1, p1 + leftSize-1);root->right = build(inorder, imid + 1, i2, postorder, p1 + leftSize, p2 - 1);return root;}};
0 0
- 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
- [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106.Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 如何利用python爬虫技术将百度贴吧上面的帖子名称爬取下来
- mongodb的聚合函数的$redact方法运用。
- Django 基础
- linux下软件的安装与卸载方法
- 【论文笔记】Text-Recognition_简略版_201606
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- java和js里面常见的正则验证
- 读者写者问题-写者优先参考答案
- 中文 iOS/Mac 开发博客列表(大
- 解决创建Maven Web项目的时候卡在installing dynamic web module facet的问题
- django admin 密码修改
- 飞速流量压缩仪app v2.2.0官方iphone版
- ElaineMOMO项目总结
- WEB、APP端问题定位测试工具