[Leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:安卓手机网络修复工具 编辑:程序博客网 时间:2024/05/13 19:23
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 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>& preorder, vector<int>& inorder) { return buildTree(begin(preorder), end(preorder), begin(inorder), end(inorder)); } TreeNode* buildTree(vector<int>::iterator pre_first, vector<int>::iterator pre_last, vector<int>::iterator in_first, vector<int>::iterator in_last) { if (pre_first == pre_last) return nullptr; if (in_first == in_last) return nullptr; auto root = new TreeNode(*pre_first); auto inrootPos = find(in_first, in_last, *pre_first); auto leftLen = distance(in_first, inrootPos); root->left = buildTree(next(pre_first), next(pre_first, leftLen + 1), in_first, next(in_first, leftLen)); root->right = buildTree(next(pre_first, leftLen + 1), pre_last, next(inrootPos), in_last); return root; }};
0 0
- 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
- 105. Construct Binary Tree from Preorder and Inorder Traversal LeetCode
- 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
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- Codeforces700A Fast As Possible(二分+找规律)
- POJ 1035 Spell checker(字典)
- LightOJ 1245 Harmonic Number (II) <思维---数学>
- Scala 元组
- PAT-B 1039. 到底买不买
- [Leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- java多线程
- 线性表
- 23种设计模式(1):单例模式
- vim 基本操作文档
- (OK) rdesktop - How to Use Remote Desktop (rdesktop) in Redhat/Fedora/CentOS
- HashMap与ConCurrentHashMap
- Linux下可执行文件的格式
- Java四舍五入取巧方式