From 5668b281a22631cc483e6a1e1a90abbdbc1f4104 Mon Sep 17 00:00:00 2001 From: sainathek Date: Wed, 11 Mar 2026 15:05:49 -0400 Subject: [PATCH] Done BFS-2 --- problem1.java | 68 +++++++++++++++++++++++++++++++++++++++++++ problem2.java | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..0d5aa801 --- /dev/null +++ b/problem1.java @@ -0,0 +1,68 @@ + /** + Time Complexity : O(N) + Explanation: + We perform a level-order traversal (BFS) and visit each node once. + + Space Complexity : O(N) + Explanation: + The queue may contain up to one level of the tree at a time. + In the worst case (complete tree) it can store about N/2 nodes. + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially unsure how to capture the rightmost node of each level. + Solved it by performing BFS level by level and storing the last + node encountered in each level traversal. + */ + +/** + * 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) { + + Queue q = new LinkedList<>(); + List li = new ArrayList<>(); + + if (root == null) return li; + + q.add(root); + + while (!q.isEmpty()) { + + int size = q.size(); + int last = 0; + + for (int i = 0; i < size; i++) { + + TreeNode t = q.poll(); + last = t.val; + + if (t.left != null) q.add(t.left); + if (t.right != null) q.add(t.right); + } + + // The last node processed at this level is the rightmost + li.add(last); + } + + return li; + } +} \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..a46d67d0 --- /dev/null +++ b/problem2.java @@ -0,0 +1,80 @@ + /** + Time Complexity : O(N) + Explanation: + We traverse the tree once using DFS to locate both nodes + and record their parent and level. + + Space Complexity : O(H) + Explanation: + Recursion stack can go up to the height of the tree (H). + In worst case (skewed tree) it can be O(N). + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially confused about the exact condition for cousins. + Later clarified that two nodes are cousins if: + 1) They are at the same level + 2) They have different parents + So while traversing the tree, I stored parent and level + for both nodes and compared them at the end. + */ + +/** + * 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 { + + int xparent = 0; + int yparent = 0; + int xlevel = 0; + int ylevel = 0; + + public boolean isCousins(TreeNode root, int x, int y) { + + dfs(root, 0, x, y); + + if (xlevel == ylevel) { + return xparent != yparent; + } + + return false; + } + + private void dfs(TreeNode root, int level, int x, int y) { + + if (root == null) return; + + // Check if x is child of current node + if ((root.left != null && root.left.val == x) || + (root.right != null && root.right.val == x)) { + + xparent = root.val; + xlevel = level + 1; + } + + // Check if y is child of current node + if ((root.left != null && root.left.val == y) || + (root.right != null && root.right.val == y)) { + + yparent = root.val; + ylevel = level + 1; + } + + dfs(root.left, level + 1, x, y); + dfs(root.right, level + 1, x, y); + } +} \ No newline at end of file