Skip to content

Commit e8d5de5

Browse files
committed
2 parents 5f8000d + 2331acd commit e8d5de5

4 files changed

Lines changed: 183 additions & 3 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Design Add and Search Words Data Structure
2+
# https://leetcode.com/problems/design-add-and-search-words-data-structure/
3+
4+
class Node(object):
5+
def __init__(self):
6+
self.value = None
7+
# key: letter value: node
8+
self.next = {}
9+
self.eow = False
10+
11+
class Trie(object):
12+
def __init__(self):
13+
self.head = Node()
14+
15+
def createNode(self, value):
16+
node = Node()
17+
node.value = value
18+
return node
19+
20+
def add(self, word, index, node):
21+
if (index == len(word)):
22+
node.eow = True
23+
return
24+
# if letter not in node next
25+
if (word[index] not in node.next):
26+
newNode = self.createNode(word[index])
27+
node.next[word[index]] = newNode
28+
# move on to next letter
29+
self.add(word, index+1, node.next[word[index]])
30+
31+
def addWord(self, word):
32+
self.add(word, 0, self.head)
33+
self.display(self.head, [])
34+
35+
def display(self, node, words):
36+
if node.eow:
37+
print "".join(words)
38+
for key in node.next:
39+
self.display(node.next[key], words+[key])
40+
41+
def searchWord(self, word):
42+
return self.search(self.head, word, 0)
43+
44+
def search(self, node, word, index):
45+
if (index == len(word)):
46+
if (node.eow):
47+
return True
48+
return False
49+
#print "checking", word[index]
50+
if (word[index] != "." and (word[index] not in node.next)):
51+
return False
52+
# dot can match any character
53+
if (word[index] == "."):
54+
for key in node.next:
55+
if(self.search(node.next[key], word, index+1)):
56+
return True
57+
else:
58+
return self.search(node.next[word[index]], word, index+1)
59+
60+
61+
class WordDictionary(object):
62+
63+
def __init__(self):
64+
self.trie = Trie()
65+
"""
66+
Initialize your data structure here.
67+
"""
68+
69+
70+
def addWord(self, word):
71+
self.trie.addWord(word)
72+
"""
73+
:type word: str
74+
:rtype: None
75+
"""
76+
77+
78+
def search(self, word):
79+
return self.trie.searchWord(word)
80+
"""
81+
:type word: str
82+
:rtype: bool
83+
"""
84+
85+
86+
87+
# Your WordDictionary object will be instantiated and called as such:
88+
# obj = WordDictionary()
89+
# obj.addWord(word)
90+
# param_2 = obj.search(word)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Lowest Common Ancestor of a Binary Search Tree
2+
# https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode(object):
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.left = None
9+
# self.right = None
10+
11+
class Solution(object):
12+
def postOrder(self, node, p, q, answer):
13+
if not node:
14+
return
15+
leftFound = self.postOrder(node.left, p, q, answer)
16+
rightFound = self.postOrder(node.right, p, q, answer)
17+
# if found in left subtree and right sub tree
18+
if (leftFound and rightFound):
19+
# then append to the answer
20+
answer.append(node)
21+
return
22+
# if one of the nodes is the value
23+
if (node == p or node == q):
24+
# then current node is lca
25+
if (leftFound or rightFound):
26+
answer.append(node)
27+
return
28+
# else return true since we found node in the sub tree.
29+
return True
30+
# if we find nothing, then we just propogate the answer from the below subtree to pass it to root.Ellipsis
31+
if (leftFound or rightFound):
32+
return True
33+
34+
35+
def lowestCommonAncestor(self, root, p, q):
36+
answer = []
37+
self.postOrder(root, p, q, answer)
38+
return answer[0]
39+
"""
40+
:type root: TreeNode
41+
:type p: TreeNode
42+
:type q: TreeNode
43+
:rtype: TreeNode
44+
"""
45+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Validate Binary Search Tree
2+
# https://leetcode.com/problems/validate-binary-search-tree/
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode(object):
6+
# def __init__(self, val=0, left=None, right=None):
7+
# self.val = val
8+
# self.left = left
9+
# self.right = right
10+
class Solution(object):
11+
# inorder traversal of BST is alwasy sorted in ascending order.
12+
def inOrder(self, node, traversal):
13+
if not node:
14+
return
15+
self.inOrder(node.left, traversal)
16+
traversal.append(node.val)
17+
self.inOrder(node.right, traversal)
18+
19+
# validate if the traversal is in ascending order or not.
20+
def validate(self, traversal):
21+
for i in range(1, len(traversal)):
22+
if (traversal[i-1] >= traversal[i]):
23+
return False
24+
return True
25+
26+
def isValidBST(self, root):
27+
traversal = []
28+
self.inOrder(root, traversal)
29+
return self.validate(traversal)
30+
"""
31+
:type root: TreeNode
32+
:rtype: bool
33+
"""
34+

Blind 75/README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,27 @@
278278
</p>
279279
</li>
280280
<li>Construct Binary Tree from Preorder and Inorder Traversal</li>
281-
<li>Validate Binary Search Tree</li>
281+
<li><a href="Programs/Validate Binary Search Tree.py">Validate Binary Search Tree</a>
282+
<p><b>Approach</b>: Do an inorder traversal of the BST. <br/>
283+
Then validate if the traversal is in ascending order or not.
284+
</p>
285+
</li>
282286
<li><a href="Programs/Kth Smallest Element in a BST.py">Kth Smallest Element in a BST</a>
283287
<p><b>Approach</b>: User inorder traversal to find the kth smallest element.
284288
</p>
285289
</li>
286-
<li>Lowest Common Ancestor of BST</li>
290+
<li><a href="Programs/Lowest Common Ancestor of a Binary Search Tree.py">Lowest Common Ancestor of a Binary Search Tree</a>
291+
<p><b>Approach</b>: First search for the nodes in left sub tree, and right sub tree. If found return the root. <br/>
292+
Don't forget to trickle the answer upwards if nothing is found. Approach similar to postOrder traversal of the tree.
293+
</p>
294+
</li>
287295
<li><a href="Programs/Implement Trie.py">Implement Trie (Prefix Tree)</a>
288296
<p><b>Thoughts</b>: You can also use this approach to serialise and de-serialise an n-ary tree.</p>
289297
</li>
290-
<li>Add and Search Word</li>
298+
<li><a href="Programs/Design Add and Search Words Data Structure.py">Add and Search Word</a>
299+
<p><b>Approach</b>: Use trie for this.
300+
</p>
301+
</li>
291302
<li>Word Search II</li>
292303
</ul>
293304
<h2>Heap (Solved all)</h2>

0 commit comments

Comments
 (0)