LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]
来源:互联网 发布:网络牛牛怎么作弊 编辑:程序博客网 时间:2024/05/17 19:59
【题目】
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
【题意】
给定先序和中序遍历,构造二叉树。题目保证所给的序列中不存在重复值
【思路】
【代码】
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int getPosOfRoot(vector<int>&inorder, int istart, int iend, int key){ for(int i=istart; i<=iend; i++){ if(inorder[i]==key)return i; } } TreeNode *build(vector<int>&preorder, int pstart, int pend, vector<int>&inorder, int istart, int iend){ if(pstart>pend)return NULL; // 创建根节点 TreeNode*root = (TreeNode*)malloc(sizeof(TreeNode)); root->val=preorder[pstart]; //确定根节点在中序遍历中的位置 int rootPos = getPosOfRoot(inorder, istart, iend, preorder[pstart]); //左子树中序遍历序列的长度 int lenLeft = rootPos-istart; //右子树中序遍历序列的长度 int lenRight = iend-rootPos; //创建左子树 TreeNode* left=build(preorder, pstart+1, pstart+lenLeft, inorder, istart, rootPos-1); //创建右子树 TreeNode* right=build(preorder, pstart+lenLeft+1, pend, inorder, rootPos+1, iend); //构造二叉树 root->left=left; root->right=right; return root; } TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { int size=preorder.size(); if(size==0)return NULL; return build(preorder, 0, size-1, inorder, 0, size-1); }};
0 3
- LeetCode(105)Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode 105] Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode 105]Construct Binary Tree from Preorder and Inorder Traversal
- leetcode || 105、Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal LeetCode 105
- Leetcode[105]-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode---(105)Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode #105 Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 105: Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal
- 【LEETCODE】105-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- 作为一个新人,怎样学习嵌入式Linux?(韦东山)
- LeetCode: Binary Tree Zigzag Level Order Traversal [103]
- SharePoint 2010 自定义字段开发
- 循环-16. 猴子吃桃问题(15)
- LeetCode: Maximum Depth of Binary Tree [104]
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]
- 个人职业规划
- android中使用SoftRefrence解决Bitmap过大内存溢出问题
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal [106]
- PMP(项目管理专业人士资格认证)
- wxWidgets+wxSmith版电子词典
- 对 Android 开发者有益的 40 条优化建议
- 帝国cms所在位置[!--newsnav--]在哪里修改?
- 当生活很艰难,你想要放弃的时候,请记住,生活充满了起起落落,如果没有低谷,那站在高处也失去了意义