【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:破解公司网络限制 编辑:程序博客网 时间:2024/06/03 15:16
题目描述:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
正序遍历顺序为从根->左孩子->右孩子,中序遍历为左孩子->根->右孩子,可以看出preorder[0]即为根,从inorder中以根为分界点划分为左子树和右子树,元素数目分别为L和R,则从preorder[1]开始L个元素即为左子树的正序遍历,preorder[1]为左子树的根;preorder[L+1]开始为右子树的正序遍历,preorder[L+1]为右子树的根。
由此可以归结为递归问题。收敛条件即L和R小于1。
代码如下:
struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder){if (preorder.empty())return NULL;TreeNode *root = new TreeNode(preorder[0]);treeNode(preorder, inorder, root, 0, 0, inorder.size() - 1);return root;}void treeNode(vector<int> &preorder, vector<int> &inorder, TreeNode *root, int rodex, int left, int right){int curr(0);for (int i = left; i < inorder.size();i++)if (inorder[i] == root->val){curr = i;break;}int llen = curr - left + 1;int rlen = right - curr + 1;if (llen > 1){root->left = new TreeNode(preorder[rodex + 1]);treeNode(preorder, inorder, root->left, rodex + 1, left, curr - 1);}if (rlen > 1){root->right = new TreeNode(preorder[rodex + llen]);treeNode(preorder, inorder, root->right, rodex + llen, curr + 1, right);}}};
0 0
- *(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
- 【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
- Redis消息核心处理流程分析
- C++中的引用
- 黑马程序员---枚举单例设计模式
- C语言实现今天倒计时
- 阿里云centos查看内存和cup的大小
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal
- NSUserDefaults的介绍与使用
- 随笔录2014.5.21
- Microsoft Visual Studio 2008 快捷键大全
- 【Misc】OAuth(解决)第三方访问私密服务(不暴露用户名/密码)
- 黑马程序员——hashcode详解
- Hbase0.96源代码之HMaster(三)Hmaster主要循环
- 八一八 微信和手Q
- 关于debug error:Damage before normal block