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
82 changes: 82 additions & 0 deletions Leetcode199.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* 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;
* }
* }
*/
// Solution - PreOrder traversal and replace left elements with right most
// elements
// TC - O(n)
// SC - O(1) - ignoring output list
// class Solution {
// List<Integer> res;

// public List<Integer> rightSideView(TreeNode root) {
// this.res = new ArrayList<>();

// helper(root, 0);
// return res;
// }

// private void helper(TreeNode node, int level) {
// // Base
// if (node == null)
// return;

// // Logic
// if (level >= res.size()) {
// res.add(node.val);
// } else {
// res.set(level, node.val);
// }

// // Left
// helper(node.left, level + 1);

// // Right
// helper(node.right, level + 1);

// }
// }

// Solution 2- DFS Solution while adding only the last element of the queue at
// each level
// TC - O(n)
// SC - O(n) - Queue space
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
if (root == null)
return res;
q.add(root);
int size = q.size();
while (!q.isEmpty()) {
while (size != 0) {
TreeNode node = q.poll();
if (size == 1) {
res.add(node.val);
}
if (node.left != null) {
q.add(node.left);
}
if (node.right != null) {
q.add(node.right);
}
size--;
}
size = q.size();
}

return res;
}
}
136 changes: 136 additions & 0 deletions Leetcode993.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* 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;
* }
* }
*/
// Solution - Using BFS
// TC - O(n)
// SC - O(n) extra space for a queue. Its never of length n buut worst case can
// have n/2 size. Hence O(n)
class Solution {
public boolean isCousins(TreeNode root, int x, int y) {
boolean res = false;
Queue<TreeNode> q = new LinkedList<>();
if (root == null)
return res;
if (root.val == x || root.val == y)
return res;
q.add(root); // add root element to the array
boolean xfound = false;
boolean yfound = false;
int xParent = -1; // Assign values of x & y parents to values of out bound
int yParent = -1;
while (!q.isEmpty()) { // iterate until q is empty.
int size = q.size();
for (int i = 0; i < size; i++) { // Process each element in the q at that level
TreeNode node = q.poll();
if (node.left != null) { // if processed node's left is not null, check if its equal to x or y and
// assign xfound,
// xParent, yfound and yParent respectively.
if (node.left.val == x) {
xfound = true;
xParent = node.val;
}
if (node.left.val == y) {
yfound = true;
yParent = node.val;
}
q.add(node.left); // add current node's left child to the q
}
if (node.right != null) { // if processed node's right is not null, check if its equal to x or y and
// assign xfound,
// xParent, yfound and yParent respectively.
if (node.right.val == x) {
xfound = true;
xParent = node.val;
}
if (node.right.val == y) {
yfound = true;
yParent = node.val;
}
q.add(node.right); // add current node's right child to the q
}
}
if (xfound && yfound) { // If both x and y are found

if (xParent == yParent) { // check thier parent's value.
return false;
} else {
return true;
}
} else if (xfound || yfound) { // If either is found, that means they are not cousins/not in same level
return false;
}

}
return true;
}
}

// Solution - Using DFS
// TC - O(n)
// SC - O(n) Recursion Stack
// class Solution {
// int xLevel;
// int yLevel;
// int xParent;
// int yParent;

// public boolean isCousins(TreeNode root, int x, int y) {
// this.xLevel = -1;
// this.yLevel = -2;
// this.xParent = -1;
// this.yParent = -2;

// helper(root, x, y, 0);
// if(xLevel == yLevel && xParent != yParent){
// return true;
// }
// return false;

// }

// private void helper(TreeNode node, int x, int y, int level) {
// // Base
// if(node == null) return;

// // Logic
// if(node.left != null) {
// if(node.left.val == x) {
// xParent = node.val;
// xLevel = level +1;
// }
// else if(node.left.val == y) {
// yParent = node.val;
// yLevel = level +1;
// } else {
// helper(node.left, x,y, level+1);
// }
// }

// if(node.right != null ) {
// if(node.right.val == x) {
// xParent = node.val;
// xLevel = level +1;
// }
// else if(node.right.val == y) {
// yParent = node.val;
// yLevel = level +1;
// } else {
// helper(node.right, x,y, level+1);
// }
// }

// }

// }