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 문제입니다.
1. 문제
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
2. Input , Output
Constraints:
- 1 <= preorder.length <= 3000
- inorder.length == preorder.length
- -3000 <= preorder[i], inorder[i] <= 3000
- preorder and inorder consist of unique values. Each value of inorder also appears in preorder.
- preorder is guaranteed to be the preorder traversal of the tree.
- inorder is guaranteed to be the inorder traversal of the tree.
3. 분류 및 난이도
Medium 난이도 문제입니다.
leetcode Top 100 Liked 문제입니다.
4. 문제 해석
- Preorder와 Inorder를 가지고 맞게 트리를 구성해야합니다.
- 재귀라는 것은 알았으나 너무 어려웠습니다. Discuss를 봤습니다.
5. code
c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int rootIdx = 0;
return build(preorder, inorder, rootIdx, 0, inorder.size()-1);
}
TreeNode* build(vector<int>& preorder, vector<int>& inorder, int& rootIdx, int left, int right) {
if (left > right) return NULL;
int pivot = left; // find the root from inorder
while(inorder[pivot] != preorder[rootIdx]) pivot++;
rootIdx++;
TreeNode* newNode = new TreeNode(inorder[pivot]);
newNode->left = build(preorder, inorder, rootIdx, left, pivot-1);
newNode->right = build(preorder, inorder, rootIdx, pivot+1, right);
return newNode;
}
};
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, preorder, inorder):
if inorder:
ind = inorder.index(preorder.pop(0))
root = TreeNode(inorder[ind])
root.left = self.buildTree(preorder, inorder[0:ind])
root.right = self.buildTree(preorder, inorder[ind+1:])
return root
6. 결과 및 후기, 개선점
코드에 대한 설명이 필요하신 분은 댓글을 달아주세요.!!
Discuss를 봤으므로 올리지 않습니다.
This post is licensed under CC BY 4.0 by the author.