From 9b7913b0cf63dbbfcb8767422500ab4e01afe331 Mon Sep 17 00:00:00 2001 From: rvuyyuru7 Date: Mon, 19 Jan 2026 16:23:42 -0600 Subject: [PATCH] BFS-2 complete --- BinaryTreeRightSideView.java | 48 +++++++++++++++++++++++++++ CousinsInBinaryTree.java | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 112 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..f24f04ac --- /dev/null +++ b/BinaryTreeRightSideView.java @@ -0,0 +1,48 @@ +/** + * 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; + * } + * } + */ +// Approach 1: BFS with queue. +// TC: O(N); N = total number of nodes +// SC: O(N) for queue +class Solution { + public List rightSideView(TreeNode root) { + List rightmostValues = new ArrayList<>(); + if (root == null) { + return rightmostValues; + } + Queue nodesByLevel = new LinkedList<>(); + nodesByLevel.offer(root); + // Iterate level by level. + while (!nodesByLevel.isEmpty()) { + int levelSize = nodesByLevel.size(); + // Iterate through nodes in the current level. + for (int i = 0; i < levelSize; i ++) { + TreeNode currentNode = nodesByLevel.poll(); + // Add left and right child nodes into nodesByLevel queue. + if (currentNode.left != null) { + nodesByLevel.offer(currentNode.left); + } + if (currentNode.right != null) { + nodesByLevel.offer(currentNode.right); + } + if (i == levelSize - 1) { + // Last node in the level or right-most node. + rightmostValues.add(currentNode.val); + } + } + } + return rightmostValues; + } +} \ No newline at end of file diff --git a/CousinsInBinaryTree.java b/CousinsInBinaryTree.java new file mode 100644 index 00000000..92f53865 --- /dev/null +++ b/CousinsInBinaryTree.java @@ -0,0 +1,64 @@ +/** + * 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 { + // Approach 1: BFS with queue + // TC: O(N); N = total number of TreeNodes + // SC: O(N) for nodesByLevel queue + public boolean isCousins(TreeNode root, int x, int y) { + if (root == null) { + return false; + } + Queue nodesByLevel = new LinkedList<>(); + nodesByLevel.offer(root); + while (!nodesByLevel.isEmpty()) { + int levelSize = nodesByLevel.size(); + boolean xFound = false; + boolean yFound = false; + for (int i = 0; i < levelSize; i ++) { + TreeNode currentNode = nodesByLevel.poll(); + if (currentNode.val == x) { + xFound = true; + } + if (currentNode.val == y) { + yFound = true; + } + // Check if x and y belong to same parent + if (currentNode.left != null && currentNode.right != null) { + if ((currentNode.left.val == x && currentNode.right.val == y) + || (currentNode.left.val == y && currentNode.right.val == x) + ) { + return false; // same parent + } + } + // Add left child to nodesByLevel queue. + if (currentNode.left != null) { + nodesByLevel.offer(currentNode.left); + } + // Add right child to nodesByLevel queue. + if (currentNode.right != null) { + nodesByLevel.offer(currentNode.right); + } + } + if (xFound && yFound) { + return true; // cousins + } + if (xFound || yFound) { + return false; // not cousins + } + } + return false; // not found + } +} \ No newline at end of file