Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Problem-1.java
Original file line number Diff line number Diff line change
@@ -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<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
helper(root, result, 0);
return result;
}
private void helper(TreeNode root, List<Integer> 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);
}
}
35 changes: 35 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -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)


49 changes: 49 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -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<TreeNode> 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;
}
}
39 changes: 39 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -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