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
50 changes: 50 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//BFS solution
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
Queue<TreeNode> q= new LinkedList<>();
if(root==null) return result;
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);
}
//process babies
if(curr.left!=null){
q.add(curr.left);
}
if(curr.right!=null){
q.add(curr.right);
}
}
}

return result;

}
}

//DFS solution
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
//if(root==null) return result;
dfs(root,0,result);
return result;
}

private void dfs(TreeNode root, int level,List<Integer> result){
//base
if(root==null) return;
//logic
if(level==result.size()){
result.add(root.val);
}
dfs(root.right,level+1,result);
dfs(root.left,level+1,result);

}
}
74 changes: 74 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//Using BFS
class Solution {
public boolean isCousins(TreeNode root, int x, int y) {
Queue <TreeNode> q= new LinkedList<>();
//pq=parent queue
Queue <TreeNode> pq=new LinkedList<>();
q.add(root);
pq.add(null);
while(!q.isEmpty()){
int size=q.size();
boolean x_found=false;
boolean y_found=false;
TreeNode x_parent=null;
TreeNode y_parent=null;
for(int i=0;i<size;i++){
TreeNode curr=q.poll();
TreeNode pcurr=pq.poll(); //pcurr=parentcurr
if(curr.val==x){
x_found=true;
x_parent=pcurr;
}
if(curr.val==y){
y_found=true;
y_parent=pcurr;
}
//process left baby
if(curr.left!=null){
q.add(curr.left);
pq.add(curr);
}
//process lright baby
if(curr.right!=null){
q.add(curr.right);
pq.add(curr);
}
}
if(x_found && y_found){
return x_parent != y_parent;
}
if(x_found || y_found){
return false;
}
}
return false;

}
}

//Using DFS
class Solution {
private int x_depth;
private int y_depth;
private TreeNode x_parent;
private TreeNode y_parent;
public boolean isCousins(TreeNode root, int x, int y) {
dfs(root,null,x,y,0);
return x_depth==y_depth && x_parent != y_parent;
}
private void dfs(TreeNode root,TreeNode papa,int x,int y,int level){
//base
if(root==null) return;
//logic
if(root.val==x){
x_depth=level;
x_parent=papa;
}
if(root.val==y){
y_depth=level;
y_parent=papa;
}
dfs(root.left,root,x,y,level+1);
dfs(root.right,root,x,y,level+1);
}
}