From 38475bc9790a49fd9c6c1cd70b63e9e71b3ebdc1 Mon Sep 17 00:00:00 2001 From: Vaishnavi Gawale Date: Sun, 25 Jan 2026 09:14:30 -0500 Subject: [PATCH] BFS-2 Done --- binary-tree-right-side-view.py | 56 ++++++++++++++ cousins-in-binary-tree.py | 131 +++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 binary-tree-right-side-view.py create mode 100644 cousins-in-binary-tree.py diff --git a/binary-tree-right-side-view.py b/binary-tree-right-side-view.py new file mode 100644 index 00000000..63847b79 --- /dev/null +++ b/binary-tree-right-side-view.py @@ -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 \ No newline at end of file diff --git a/cousins-in-binary-tree.py b/cousins-in-binary-tree.py new file mode 100644 index 00000000..3a940441 --- /dev/null +++ b/cousins-in-binary-tree.py @@ -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 + + + + + + + + + \ No newline at end of file