From e868e61175cbf4bd0fbe726a5d5160bbff184ad8 Mon Sep 17 00:00:00 2001 From: Sahithipsl470 Date: Wed, 11 Feb 2026 19:16:17 -0500 Subject: [PATCH] "BFS-2 done" --- Problem-1.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ Problem-1.py | 35 +++++++++++++++++++++++++++++++++++ Problem-2.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ Problem-2.py | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 Problem-1.java create mode 100644 Problem-1.py create mode 100644 Problem-2.java create mode 100644 Problem-2.py diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 00000000..cb2b0c91 --- /dev/null +++ b/Problem-1.java @@ -0,0 +1,46 @@ +// Time Complexity :O(N) +// Space Complexity :O(H)..H= height of the tree +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this :no + +// Your code here along with comments explaining your approach +// We do a modified pre-order DFS (root -> right -> left) to ensure we visit the rightmost nodes first at each level. +// When visiting a new level for the first time, we append that node’s value to the result. +// This gives the right-side view of the binary tree. + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public List rightSideView(TreeNode root) { + List result = new ArrayList<>(); + helper(root, result, 0); + return result; + } + private void helper(TreeNode root, List result, int level){ + //base + if (root==null){ + return; + } + + //logic + if (result.size() == level){ + result.add(root.val); + } + + helper(root.right,result,level+1); + helper(root.left, result, level+1); + } +} \ No newline at end of file diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..272f40f5 --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,35 @@ +# Time Complexity :O(N) +# Space Complexity :O(H)..H= height of the tree +# Did this code successfully run on Leetcode :yes +# Any problem you faced while coding this :no + +# Your code here along with comments explaining your approach +# We do a modified pre-order DFS (root -> right -> left) to ensure we visit the rightmost nodes first at each level. +# When visiting a new level for the first time, we append that node’s value to the result. +# This gives the right-side view of the binary tree. + +# 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]: + result = [] + self.helper(root, result,0) + return result + def helper(self, root,result,level): + #base + if not root: + return + + #logic + if len(result) == level: + result.append(root.val) + + #recursion + self.helper(root.right,result,level+1) + self.helper(root.left,result,level+1) + + \ No newline at end of file diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 00000000..4d13bb40 --- /dev/null +++ b/Problem-2.java @@ -0,0 +1,49 @@ +// Time Complexity :O(N) +// Space Complexity :O(N) +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this :no + +// Your code here along with comments explaining your approach +/* +We perform a level-order traversal (BFS) to process nodes depth by depth. +At each level, we check whether both x and y are present and ensure they are not siblings (same parent). +If both are found at the same level and are not siblings, they are cousins; otherwise, they are not. +*/ + + +import java.util.*; + +class Solution { + public boolean isCousins(TreeNode root, int x, int y) { + Queue queue = new LinkedList<>(); + queue.offer(root); + + while (!queue.isEmpty()) { + int size = queue.size(); + boolean foundX = false; + boolean foundY = false; + + for (int i = 0; i < size; i++) { + TreeNode node = queue.poll(); + + if (node.val == x) foundX = true; + if (node.val == y) foundY = true; + + if (node.left != null && node.right != null) { + if ((node.left.val == x && node.right.val == y) || + (node.left.val == y && node.right.val == x)) { + return false; + } + } + + if (node.left != null) queue.offer(node.left); + if (node.right != null) queue.offer(node.right); + } + + if (foundX && foundY) return true; + if (foundX || foundY) return false; + } + + return false; + } +} diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..d90aba8f --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,39 @@ +# Time Complexity :O(N) +# Space Complexity :O(N) +# Did this code successfully run on Leetcode :yes +# Any problem you faced while coding this :no + +# Your code here along with comments explaining your approach +# We perform a level-order traversal (BFS) to process nodes depth by depth. +# At each level, we check whether both x and y are present and ensure they are not siblings (same parent). +# If both are found at the same level and are not siblings, they are cousins; otherwise, they are not. + +class Solution: + def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: + queue = deque([(root, None)]) + + while queue: + size = len(queue) + x_parent = None + y_parent = None + + for _ in range(size): + node, parent = queue.popleft() + + if node.val == x: + x_parent = parent + if node.val == y: + y_parent = parent + + if node.left: + queue.append((node.left, node)) + if node.right: + queue.append((node.right, node)) + + if x_parent or y_parent: + return ( + x_parent is not None and + y_parent is not None and + x_parent != y_parent + ) + return False