Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions binary-tree-right-side-view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#-------------SOlution 1 : BFS-------------
''' Time Complexity : O(n)
Space Complexity : O(n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Approach : 1. Traverse the tree level wise,
2. append the last element of each level to the list
'''
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
q = deque()
result = []
q.append(root)
while q:
size = len(q)
for i in range(size):
curr = q.popleft()
#if last element in the level
if i == size-1:
result.append(curr.val)
if curr.left:
q.append(curr.left)
if curr.right:
q.append(curr.right)
return result


#-------------SOlution 2 : DFS-------------
''' Time Complexity : O(n)
Space Complexity : O(h)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Approach : 1. Maintain result list, traverse the tree in preorder,
2. store the node at respective index in result
'''
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
result = []
def helper(root, level):
if root is None:
return
if level == len(result):
result.append(root.val)
else:
result[level] = root.val
helper(root.left,level+1)
helper(root.right,level+1)

helper(root, 0)
return result
131 changes: 131 additions & 0 deletions cousins-in-binary-tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#------------ Solution 1 : BFS - Maintaining Parent Queue--------
''' Time Complexity : O(n)
Space Complexity : O(n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

'''
class Solution:
def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool:
xfound,yfound = False, False
xparent, yparent = 0, 0
q = deque()
pq = deque()
q.append(root)
pq.append(root)
while q:
size = len(q)
for i in range(size):
curr = q.popleft()
currp = pq.popleft()
if curr.val == x:
xfound = True
xparent = currp
if curr.val == y:
yfound = True
yparent = currp

if xfound and yfound:
if xparent != yparent:
return True

if curr.left:
q.append(curr.left)
pq.append(curr)
if curr.right:
q.append(curr.right)
pq.append(curr)

if xfound or yfound:
return False

#------------ Solution 1 : BFS - No Parent queue - Checking if sibling--------
''' Time Complexity : O(n)
Space Complexity : O(n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''
class Solution:
def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool:
xfound , yfound = False, False
q = deque()
q.append(root)
while q:
size = len(q)
for i in range(size):
curr = q.popleft()

if curr.left is not None and curr.right is not None:
if curr.left.val == x and curr.right.val == y:
return False
if curr.left.val == y and curr.right.val == x:
return False

if curr.val == x:
xfound = True
if curr.val == y:
yfound = True

if curr.left:
q.append(curr.left)
if curr.right:
q.append(curr.right)

if xfound and yfound:
return True
if xfound or yfound:
return False



#---------Soultion 3 : DFS : Maintain global level and Check siblings -------
''' Time Complexity : O(n)
Space Complexity : O(h)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

'''
class Solution:
def __init__(self):
self.xfound , self.yfound = False, False
self.xlevel , self.ylevel = -1, -1
self.result = True

def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool:

def helper(root, level):
#base
if root is None:
return
#logic
if root.left is not None and root.right is not None:
if root.left.val == x and root.right.val == y:
self.result = False
if root.left.val == y and root.right.val == x:
self.result = False

if root.val == x :
self.xfound = True
self.xlevel = level
if root.val == y :
self.yfound = True
self.ylevel = level

helper(root.left,level+1)
helper(root.right,level+1)

if self.xfound and self.yfound:
if self.xlevel != self.ylevel:
self.result = False

helper(root, 0)
return self.result