use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class NovemberW4 method validateBSTUsingStack.
// Tree Inorder Traversal
public boolean validateBSTUsingStack(TreeNode root) {
TreeNode prev = null;
if (root == null)
return true;
Stack<TreeNode> stack = new Stack<TreeNode>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
if (prev != null && prev.val >= root.val)
return false;
prev = root;
root = root.right;
}
return true;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class NovemberW4 method isCousinsBfs.
public boolean isCousinsBfs(TreeNode root, int x, int y) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
TreeNode xParent = null;
TreeNode yParent = null;
while (!q.isEmpty()) {
int size = q.size();
while (size-- > 0) {
TreeNode curr = q.poll();
if (curr.left != null) {
if (curr.left.val == x) {
xParent = curr;
} else if (curr.left.val == y) {
yParent = curr;
}
q.offer(curr.left);
}
if (curr.right != null) {
if (curr.right.val == x) {
xParent = curr;
} else if (curr.right.val == y) {
yParent = curr;
}
q.offer(curr.right);
}
if (xParent != null && yParent != null) {
break;
}
}
if (xParent != null && yParent != null) {
return xParent != yParent;
}
if ((xParent == null && yParent != null) || (xParent != null && yParent == null)) {
return false;
}
}
return false;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class NovemberW4 method distanceK.
/*
863. All Nodes Distance K in Binary Tree
https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
ToDo : DFS
Important
*/
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
List<Integer> res = new ArrayList<>();
if (root == null || K < 0)
return res;
buildMap(root, null);
if (!map.containsKey(target))
return res;
Set<TreeNode> visited = new HashSet<>();
visited.add(target);
Queue<TreeNode> q = new LinkedList<>();
q.offer(target);
while (!q.isEmpty()) {
if (K == 0) {
while (!q.isEmpty()) {
res.add(q.poll().val);
}
}
int size = q.size();
while (size-- > 0) {
TreeNode curr = q.poll();
visited.add(curr);
List<TreeNode> neighbours = map.get(curr);
for (TreeNode adj : neighbours) {
if (visited.contains(adj))
continue;
q.offer(adj);
visited.add(adj);
}
}
K--;
}
return res;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class OctoberW1 method allPossibleFBTRec.
/**
* https://leetcode.com/problems/all-possible-full-binary-trees/
*/
public List<TreeNode> allPossibleFBTRec(int N) {
List<TreeNode> res = new ArrayList<>();
if (N == 1) {
res.add(new TreeNode(0));
return res;
}
for (int leftNum = 1; leftNum <= N - 1; leftNum += 2) {
List<TreeNode> left = allPossibleFBTRec(leftNum);
List<TreeNode> right = allPossibleFBTRec(N - leftNum - 1);
for (TreeNode currLeft : left) {
for (TreeNode currRight : right) {
TreeNode cur = new TreeNode(0);
cur.left = currLeft;
cur.right = currRight;
res.add(cur);
}
}
}
return res;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class OctoberW1 method allPossibleFBTMemo.
public List<TreeNode> allPossibleFBTMemo(int N, List<TreeNode>[] memo) {
List<TreeNode> res = new ArrayList<>();
if (N == 1) {
res.add(new TreeNode(0));
return res;
}
if (memo[N] != null) {
return memo[N];
}
for (int leftNum = 1; leftNum <= N - 1; leftNum += 2) {
List<TreeNode> left = allPossibleFBTMemo(leftNum, memo);
List<TreeNode> right = allPossibleFBTMemo(N - leftNum - 1, memo);
for (TreeNode currLeft : left) {
for (TreeNode currRight : right) {
TreeNode cur = new TreeNode(0);
cur.left = currLeft;
cur.right = currRight;
res.add(cur);
}
}
}
memo[N] = res;
return res;
}
Aggregations