[Leetcode] #105 Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:0基础学java好学吗 编辑:程序博客网 时间:2024/05/23 12:25
Discription:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Solution:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder, int ps, int pe, int is, int ie){if (ps > pe) return NULL;TreeNode *root = new TreeNode(preorder[ps]);int x;for (int i = is; i <= ie; i++){if (inorder[i] == root->val){x = i;break;}}root->left = buildTree(preorder, inorder, ps + 1, ps + x - is, is, x - 1);root->right = buildTree(preorder, inorder, pe - ie + x + 1, pe, x + 1, ie);return root;}TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {return buildTree(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);}
附:Leetcode源代码见我的GitHub
0 0
- 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
- CCF201609-3 炉石传说(100分)
- 从自签名证书导出pfx和cer证书
- Qt下QTableWidget的使用
- Biztalk AS2开发经验总结
- Java中static变量作用和用法详解
- [Leetcode] #105 Construct Binary Tree from Preorder and Inorder Traversal
- 算法训练 最大的算式 蓝桥杯
- java中修饰符关键词
- Maven私服搭建以及构建index
- 电脑最大支持内存
- TCP TIME_WAIT过多的解决方法
- JavaScript练习之删除记录
- Java23种设计模式
- C# 数组、ArrayList、List、Dictionary的用法与区别