Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
2018-02-08 09:11
323 查看
原题:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, givenpreorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]Return the following binary tree: 3
/ \
9 20
/ \
15 7解决方法:
- 前序遍历的第一个节点是根节点。
- 根据根节点把中序遍历的点分成左右两部分。
- 根据中序遍历左右部分的长度将前序遍历也分割成左右两部分。
这样递归循环即可求出最后的树。
代码:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, givenpreorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]Return the following binary tree: 3
/ \
9 20
/ \
15 7解决方法:
- 前序遍历的第一个节点是根节点。
- 根据根节点把中序遍历的点分成左右两部分。
- 根据中序遍历左右部分的长度将前序遍历也分割成左右两部分。
这样递归循环即可求出最后的树。
代码:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder, int pre_start, int pre_stop, int in_start, int in_stop){ if (pre_stop < pre_start){ return NULL; } TreeNode* root = new TreeNode(preorder[pre_start]); int pos; for(int i = in_start; i <= in_stop;i++){ if (preorder[pre_start] == inorder[i]){ pos = i; break; } } root->left = buildTree(preorder, inorder, pre_start + 1, pre_start + pos - in_start, in_start, pos - 1); root->right = buildTree(preorder, inorder, pre_stop - in_stop + pos + 1, pre_stop, pos + 1, in_stop); return root; } TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { return buildTree(preorder, inorder, 0, preorder.size()-1, 0, inorder.size() - 1); }
相关文章推荐
- [Leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode:M-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@python
- 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_middle_95_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-java-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