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
68 changes: 68 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -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<Integer> rightSideView(TreeNode root) {

Queue<TreeNode> q = new LinkedList<>();
List<Integer> 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;
}
}
80 changes: 80 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -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);
}
}