From ca2ac8dd087af64e35529ab94dcb058ee61b6cbe Mon Sep 17 00:00:00 2001 From: Megha Manjunath Raykar Date: Sat, 5 Nov 2022 22:52:10 +0530 Subject: [PATCH 1/4] Added files for BT Right Side View and Cousins in BT --- BT-Cousins.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ BT-RightSideView.py | 30 +++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 BT-Cousins.py create mode 100644 BT-RightSideView.py diff --git a/BT-Cousins.py b/BT-Cousins.py new file mode 100644 index 00000000..4c8cba0b --- /dev/null +++ b/BT-Cousins.py @@ -0,0 +1,49 @@ +#Time Complexity : O(n) +#Space Complexity : O(n) + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: + if root == None or x == y: + return False + + q = [] + q.append(root) + x_found = False + y_found = False + + while q: + size = len(q) + for i in range(0, size): + curr = q.pop(0) + if curr.val == x: + x_found = True + + if curr.val == y: + y_found = True + + if curr.left != None and curr.right != 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.left != None: + q.append(curr.left) + + if curr.right != None: + q.append(curr.right) + + if x_found == True and y_found == True: + return True + + if x_found == True or y_found == True: + return False + + return False \ No newline at end of file diff --git a/BT-RightSideView.py b/BT-RightSideView.py new file mode 100644 index 00000000..8774b9ec --- /dev/null +++ b/BT-RightSideView.py @@ -0,0 +1,30 @@ +#Time Complexity : O(n) +#Space Complexity : O(n) + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def rightSideView(self, root: Optional[TreeNode]) -> List[int]: + + if root == None: + return [] + + result = [] + q = [root] + + while q: + size = len(q) + for i in range(0, size): + curr = q.pop(0) + if i == size - 1: + result.append(curr.val) + if curr.left != None: + q.append(curr.left) + if curr.right != None: + q.append(curr.right) + + return result \ No newline at end of file From c6e35a99b0cf5ad78389b4154032dc5e7f7dee3f Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Sat, 14 Mar 2026 20:09:12 -0700 Subject: [PATCH 2/4] Problem1 added --- BT-Cousins.py | 49 --------------------------------------------- BT-RightSideView.py | 30 --------------------------- Probem1.py | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 79 deletions(-) delete mode 100644 BT-Cousins.py delete mode 100644 BT-RightSideView.py create mode 100644 Probem1.py diff --git a/BT-Cousins.py b/BT-Cousins.py deleted file mode 100644 index 4c8cba0b..00000000 --- a/BT-Cousins.py +++ /dev/null @@ -1,49 +0,0 @@ -#Time Complexity : O(n) -#Space Complexity : O(n) - -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right -class Solution: - def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: - if root == None or x == y: - return False - - q = [] - q.append(root) - x_found = False - y_found = False - - while q: - size = len(q) - for i in range(0, size): - curr = q.pop(0) - if curr.val == x: - x_found = True - - if curr.val == y: - y_found = True - - if curr.left != None and curr.right != 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.left != None: - q.append(curr.left) - - if curr.right != None: - q.append(curr.right) - - if x_found == True and y_found == True: - return True - - if x_found == True or y_found == True: - return False - - return False \ No newline at end of file diff --git a/BT-RightSideView.py b/BT-RightSideView.py deleted file mode 100644 index 8774b9ec..00000000 --- a/BT-RightSideView.py +++ /dev/null @@ -1,30 +0,0 @@ -#Time Complexity : O(n) -#Space Complexity : O(n) - -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right -class Solution: - def rightSideView(self, root: Optional[TreeNode]) -> List[int]: - - if root == None: - return [] - - result = [] - q = [root] - - while q: - size = len(q) - for i in range(0, size): - curr = q.pop(0) - if i == size - 1: - result.append(curr.val) - if curr.left != None: - q.append(curr.left) - if curr.right != None: - q.append(curr.right) - - return result \ No newline at end of file diff --git a/Probem1.py b/Probem1.py new file mode 100644 index 00000000..cd599c0a --- /dev/null +++ b/Probem1.py @@ -0,0 +1,41 @@ +# https://leetcode.com/problems/binary-tree-right-side-view/description/ + +# Time Complexity: O(n) +# Space Complexity: O(n) + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def rightSideView(self, root: Optional[TreeNode]) -> List[int]: + q = deque() + res = [] + + if root is None: + return [] + + q.append(root) + + while q: + size = len(q) + print(size) + + for i in range(size): + qroot = q.popleft() + + # if i == size - 1: + # res.append(qroot.val) + + if qroot.left is not None: + q.append(qroot.left) + + if qroot.right is not None: + q.append(qroot.right) + + if i == size - 1: + res.append(qroot.val) + + return res \ No newline at end of file From 750b7fb9325b1cb1cb5c8a098932a5b4b8150c2a Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Sat, 14 Mar 2026 20:48:19 -0700 Subject: [PATCH 3/4] Problem2 added --- Problem2.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Problem2.py diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..8b0100b9 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,64 @@ +# https://leetcode.com/problems/cousins-in-binary-tree/description/ + +# Time Complexity: O(n) +# Space Complexity: O(n) + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: + q = deque() + pq = deque() + + x_found = False + y_found = False + x_parent = None + y_parent = None + + if root is None: + return False + + q.append(root) + pq.append(None) + + while q: + size = len(q) + for i in range(size): + qroot = q.popleft() + pqroot = pq.popleft() + + if qroot.val == x: + x_found = True + x_parent = pqroot + + if qroot.val == y: + y_found = True + y_parent = pqroot + + if qroot.left is not None: + q.append(qroot.left) + pq.append(qroot) + + if qroot.right is not None: + q.append(qroot.right) + pq.append(qroot) + + if x_found and y_found and x_parent != y_parent: + return True + elif x_found and y_found and x_parent == y_parent: + return False + elif x_found or y_found: + return False + + return False + + + + + + + \ No newline at end of file From 0a09aaebb7d2a0a1ccfc9a5e5e1db6a8d8f49a9a Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Tue, 17 Mar 2026 22:40:59 -0700 Subject: [PATCH 4/4] Problem1 and Problem2 added --- Problem2.py => cousins.py | 0 Probem1.py => right-side-view.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Problem2.py => cousins.py (100%) rename Probem1.py => right-side-view.py (100%) diff --git a/Problem2.py b/cousins.py similarity index 100% rename from Problem2.py rename to cousins.py diff --git a/Probem1.py b/right-side-view.py similarity index 100% rename from Probem1.py rename to right-side-view.py