leetcode之Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:通用数据采集软件 编辑:程序博客网 时间:2024/05/13 05:20
题目:
Given preorder and inorder traversal of a tree, construct the binary 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* maketree(vector<int>& preorder, vector<int>& inorder, int pstart, int pend, int instart, int inend) {if (pstart > pend || instart > inend) return NULL;TreeNode* a = new TreeNode(preorder[pstart]);if (pstart == pend&&instart == inend) return a;int location;for (int i = instart; i <= inend; i++) {if (inorder[i] == preorder[pstart]) {location = i;break;}}a->left = maketree(preorder, inorder, pstart + 1, location-instart+pstart, instart, location - 1);a->right = maketree(preorder, inorder, pstart+location-instart+1, pend, location + 1, inend);return a;}TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {if (preorder.empty()) return NULL;TreeNode* a = new TreeNode(preorder[0]);int location;for (int i = 0; i < inorder.size(); i++) {if (inorder[i] == preorder[0]) {location = i;break;}}a->left = maketree(preorder, inorder, 1, location, 0, location-1);a->right = maketree(preorder, inorder, location + 1, inorder.size() - 1, location + 1, inorder.size() - 1);return a;}};
0 0
- leetcode之Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode之Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode之Construct Binary Tree from Preorder and Inorder Traversal
- leetcode之Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode之Construct Binary Tree from Preorder and Inorder Traversal
- leetcode之Construct Binary Tree from Preorder and Inorder Traversal
- 【Leetcode】之Construct Binary Tree from Preorder and Inorder Traversal
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- hadoop 视频总结(1) -- 主要是概念
- 在Android 6.0下继续使用HttpClient
- hdu 5087(LIS变形)
- java多线程1
- Valgrind内存管理工具
- leetcode之Construct Binary Tree from Preorder and Inorder Traversal
- .NET Framework 各版本区别
- 1009. Product of Polynomials (25)
- 双击及多点击事件手势使用
- 常用的正则表达式
- 解决Ubuntu16.04的WPS无法使用搜狗拼音输入法
- 线程绑定CPU核
- vs2010打开vs2012,vs2013编写的工程
- 勾股定理一日一证连载35