From bda7c97aa8b087d89682e717413e2786d874ffc6 Mon Sep 17 00:00:00 2001 From: Sarvani Baru Date: Thu, 5 Mar 2026 16:33:51 -0800 Subject: [PATCH] Done BFS-2 --- BinaryTreeRightSideView.java | 48 ++++++++++++++++++++++++++++ CousinsInBinaryTree.java | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 BinaryTreeRightSideView.java create mode 100644 CousinsInBinaryTree.java diff --git a/BinaryTreeRightSideView.java b/BinaryTreeRightSideView.java new file mode 100644 index 00000000..46dc280f --- /dev/null +++ b/BinaryTreeRightSideView.java @@ -0,0 +1,48 @@ +// 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 +/* +Perform level order traversal using queue. The idea is to add rightmost element at each level, so we compare +if we reached end of the size at each level and add its children further to the queue. During each iteration, +we add the rightmost element to our result. + */ +/** + * 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<>(); + if(root == null) + return result; + Queue q = new LinkedList<>(); + q.add(root); + while(!q.isEmpty()) { + int size = q.size(); + for(int i = 0 ; i < size ; i++) { + TreeNode curr = q.poll(); + if(i == size - 1) + result.add(curr.val); + if(curr.left != null) + q.add(curr.left); + if(curr.right != null) + q.add(curr.right); + } + } + return result; + } +} \ No newline at end of file diff --git a/CousinsInBinaryTree.java b/CousinsInBinaryTree.java new file mode 100644 index 00000000..912438a3 --- /dev/null +++ b/CousinsInBinaryTree.java @@ -0,0 +1,62 @@ +// 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 +/* +The main idea is that if 2 nodes have same parent and do not share same level, then they are not cousins.So, +we iteratively check through a queue if the incoming node's children equal to x and y, then return false as +same parent.Similarly, we update 2 boolean found variables when x and y are found. Since we are doing level +order traversal, if the found variables do not get updated at same level, that means they are not cousins. + */ +/** + * 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 boolean isCousins(TreeNode root, int x, int y) { + Queue q = new LinkedList<>(); + q.add(root); + + boolean xFound = false, yFound = false; + + while(!q.isEmpty()) { + int size = q.size(); + for(int i = 0 ; i < size ; i++) { + TreeNode curr = q.poll(); + if(curr.left != null && curr.right != null) { + if(curr.left.val == x && curr.right.val == y) + return false; + if(curr.left.val == y && curr.right.val == x) + return false; + } + + if(curr.val == x) + xFound = true; + if(curr.val == y) + yFound = true; + if(curr.left != null) + q.add(curr.left); + if(curr.right != null) + q.add(curr.right); + } + if(xFound && yFound) + return true; + if(xFound || yFound) + return false; + } + return false; + } +} \ No newline at end of file