From 892c9b3a36b5501cebf668aba5b183012923b5d7 Mon Sep 17 00:00:00 2001 From: Adithya Vinayak Date: Sat, 7 Mar 2026 22:29:27 -0500 Subject: [PATCH] working solution --- problem1.py | 22 ++++++++++++++++++++++ problem2.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 problem1.py create mode 100644 problem2.py diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..21d7a16e --- /dev/null +++ b/problem1.py @@ -0,0 +1,22 @@ +# problem 1 + +# 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]: + self.final_arr = [] + self.helper(root,0) + return self.final_arr + + def helper(self,root,depth): + if not root: + return + if len(self.final_arr) == depth: + self.final_arr.append(float('-inf')) # + self.helper(root.left,depth+1) + self.final_arr[depth] = root.val + self.helper(root.right,depth+1) \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..9fa11215 --- /dev/null +++ b/problem2.py @@ -0,0 +1,35 @@ +# problem 2 + +# 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 helper(self,root,x,y,depth,parent): + if not root: + return + if x == root.val: + self.x_depth = depth + self.x_parent = parent + + if y == root.val: + self.y_depth = depth + self.y_parent = parent + + self.helper(root.left,x,y,depth+1,root) + self.helper(root.right,x,y,depth+1,root) + + def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: + self.x_parent = None + self.y_parent = None + self.x_depth = -1 + self.y_depth = -1 + + self.helper(root,x,y,0,None) + + if self.x_parent != self.y_parent and self.x_depth == self.y_depth: + return True + return False