Skip to content

Commit a93ac1e

Browse files
author
Partho Biswas
committed
1008_Construct_Binary_Search_Tree_from_Preorder_Traversal
1 parent b362250 commit a93ac1e

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
383383
|09| [110. Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)| [Python](https://tinyurl.com/wu6rdaw/110_Balanced_Binary_Tree.py)| [Video 1](https://www.youtube.com/watch?v=LU4fGD-fgJQ), [Article 1](https://leetcode.com/problems/balanced-binary-tree/discuss/36042/Two-different-definitions-of-balanced-binary-tree-result-in-two-different-judgments), [Article 2](https://leetcode.com/problems/balanced-binary-tree/discuss/35708/VERY-SIMPLE-Python-solutions-(iterative-and-recursive)-both-beat-90) | Easy | --- |
384384
|10| [105. Construct Binary Tree from Preorder and Inorder Traversal](https://tinyurl.com/y4s62xbu)| [Python](https://tinyurl.com/wu6rdaw/105_Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.py)| [Art 1](https://tinyurl.com/y3megvpm), [Art 2](https://tinyurl.com/tazaoyz), [Vid 1](https://tinyurl.com/qoqdsqv) | Medium | DFS, Very Important |
385385
|11| [106. Construct Binary Tree from Inorder and Postorder Traversal](https://tinyurl.com/yx78og6g)| [Python](https://tinyurl.com/wu6rdaw/106_Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.py)| [Art 1](https://tinyurl.com/vlntqrp) | Medium | DFS, Very Important |
386-
|12| **[889. Construct Binary Tree from Preorder and Postorder Traversal](https://tinyurl.com/y578ty5u)** | [Python](https://tinyurl.com/wu6rdaw/889_Construct_Binary_Tree_from_Preorder_and_Postorder_Traversal.py)| [Vid 1](https://tinyurl.com/r6d4ym5), [Vid 2](https://tinyurl.com/r6d4ym5), **[Art 1](https://tinyurl.com/tflqhp9)**, [Art 2](https://tinyurl.com/wkq7qyu) | Medium | 📌 **TODO: Check again. Important** |
386+
|12| **[889. Construct Binary Tree from Preorder and Postorder Traversal](https://tinyurl.com/y578ty5u)** | [Python](https://tinyurl.com/wu6rdaw/889_Construct_Binary_Tree_from_Preorder_and_Postorder_Traversal.py)| [Vid 1](https://tinyurl.com/r6d4ym5), **[Art 1](https://tinyurl.com/tflqhp9)**, [Art 2](https://tinyurl.com/wkq7qyu) | Medium | 📌 **TODO: Check again. Important** |
387387
|13| **[124. Binary Tree Maximum Path Sum](https://tinyurl.com/yycvxmmy)**| [Python](https://tinyurl.com/wu6rdaw/124_Binary_Tree_Maximum_Path_Sum.py)| [Art 1](https://tinyurl.com/uocr9np), [Art 2](https://tinyurl.com/s23sqhe), [Vid 1](https://tinyurl.com/wwe95qx), [AlgoExpert](https://tinyurl.com/vofbonm) | Very Hard | **Very important and tricky** |
388388
|14| **[979. Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree/)**| [Python](https://tinyurl.com/wu6rdaw/979_Distribute_Coins_in_Binary_Tree.py)| [Art 1](https://tinyurl.com/rugoa38), [Art 2](https://tinyurl.com/whkdbl4), [Art 3](https://tinyurl.com/s62fws7) | Medium | **Very important. Postorder DFS** |
389389
|15| [116. Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)| [Python](https://tinyurl.com/wu6rdaw/116_Populating_Next_Right_Pointers_in_Each_Node.py)| [Art 1](https://tinyurl.com/t6p8kjb), [Art 2](https://tinyurl.com/ujzrhkj), **[Art 3](https://tinyurl.com/unrow77)** | Medium | BFS/DFS, Level order traversal |
@@ -428,6 +428,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
428428
|10| [109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)| [Python](https://tinyurl.com/wu6rdaw/109_Convert_Sorted_List_to_Binary_Search_Tree.py)| --- | Medium | Classic problem. Very important |
429429
|11| **[729. My Calendar I](https://tinyurl.com/r3ew2lb)** | [Python](https://tinyurl.com/wu6rdaw/729_My_Calendar_I.py), [Swift](https://tinyurl.com/wuja3c4/729_My_Calendar_I.swift)| [Art 1](https://tinyurl.com/tbd2z7u), [Art 2](https://tinyurl.com/t35fp3e) | Medium | Use self balancing BST for O(nlogn) solution. **Very important** |
430430
|12| **[449. Serialize and Deserialize BST](https://tinyurl.com/rx9rn3l)** | [Python](https://tinyurl.com/wu6rdaw/449_Serialize_and_Deserialize_BST.py), [Swift](https://tinyurl.com/wuja3c4/449_Serialize_and_Deserialize_BST.swift)| [Art 1](https://tinyurl.com/t6e2b97) | Medium | --- |
431+
|13| **[1008. Construct Binary Search Tree from Preorder Traversal](https://tinyurl.com/y6less2m)** | [Python](https://tinyurl.com/wu6rdaw/1008_Construct_Binary_Search_Tree_from_Preorder_Traversal.py), [Swift](https://tinyurl.com/wuja3c4/1008_Construct_Binary_Search_Tree_from_Preorder_Traversal.swift)| [Art 1](https://tinyurl.com/y9mondek), [Art 2](https://tinyurl.com/y8byt732), [Art 3](https://tinyurl.com/yaq7w2jt), [Art 4](https://tinyurl.com/ybwtzpzo) | Medium | --- |
431432

432433
</p>
433434
</details>

algoexpert.io/python/Caesar_Cipher_Encryptor.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
# Solution 1
44
# O(n) time | O(n) space
5-
def caesar_cipher_encryptor(string, key):
5+
def caesarCipherEncryptor(string, key):
66
new_letters = []
77
new_key = key % 26
8-
for letters in string:
9-
new_letters.append(get_new_letter(letters, string))
8+
for letter in string:
9+
new_letters.append(get_new_letter(letter, new_key))
1010
return "".join(new_letters)
1111

1212
def get_new_letter(letter, key):
1313
newLetterCode = ord(letter) + key
1414
return chr(newLetterCode) if newLetterCode <= 122 else chr(96 + newLetterCode % 122)
1515

1616

17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def bstFromPreorder(self, preorder):
10+
"""
11+
:type preorder: List[int]
12+
:rtype: TreeNode
13+
"""
14+
inorder = sorted(preorder)
15+
inorderMap = {val: i for i, val in enumerate(inorder)}
16+
return self.bstFromPreorderHelper(preorder, inorderMap, 0, len(inorder) - 1)
17+
18+
def bstFromPreorderHelper(self, preorder, inorderMap, inorderLeftIdx, inorderRightIdx):
19+
if not preorder:
20+
return None
21+
rootVal = preorder.pop(0)
22+
rootIdxInInorder = inorderMap[rootVal]
23+
root = TreeNode(rootVal)
24+
if inorderLeftIdx < rootIdxInInorder:
25+
root.left = self.bstFromPreorderHelper(preorder, inorderMap, inorderLeftIdx, rootIdxInInorder - 1)
26+
if inorderRightIdx > rootIdxInInorder:
27+
root.right = self.bstFromPreorderHelper(preorder, inorderMap, rootIdxInInorder + 1, inorderRightIdx)
28+
return root

0 commit comments

Comments
 (0)