[LeetCode][Python3] 105. Construct Binary Tree from Preorder and Inorder Traversal
2019. 3. 24. 01:27 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
My Solution :
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
def build(preorder, inorder, left, right):
if left < right:
root = TreeNode(preorder[self.pre_idx])
root_idx = inorder.index(root.val, left, right)
self.pre_idx += 1
root.left = build(preorder, inorder, left, root_idx)
root.right = build(preorder, inorder, root_idx+1, right)
return root
self.pre_idx = 0
return build(preorder, inorder, 0, len(inorder))
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 329. Longest Increasing Path in a Matrix (0) | 2019.03.27 |
---|---|
[LeetCode][Python3] 264. Ugly Number II (0) | 2019.03.26 |
[LeetCode][Python3] 334. Increasing Triplet Subsequence (0) | 2019.03.25 |
[LeetCode][Python3] 297. Serialize and Deserialize Binary Tree (0) | 2019.03.25 |
[LeetCode][Python3] 131. Palindrome Partitioning (0) | 2019.03.23 |
[LeetCode][Python3] 128. Longest Consecutive Sequence (0) | 2019.03.23 |
[LeetCode][Python3] 42. Trapping Rain Water (0) | 2019.03.23 |
[LeetCode][Python] 341. Flatten Nested List Iterator (0) | 2019.03.22 |
최근에 달린 댓글 최근에 달린 댓글