use of com.pkumar7.trees.RootingTree.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class NovemberW3 method formBstStack.
public TreeNode formBstStack(int[] preOrder) {
Stack<TreeNode> stack = new Stack<>();
TreeNode root = new TreeNode(preOrder[0]);
stack.push(root);
for (int idx = 1; idx < preOrder.length; idx++) {
int curr_ele = preOrder[idx];
TreeNode curr_node = new TreeNode(preOrder[idx]);
if (!stack.isEmpty() && curr_ele < stack.peek().val) {
stack.peek().left = curr_node;
} else {
TreeNode parent = stack.peek();
while (!stack.isEmpty() && curr_ele > stack.peek().val) {
parent = stack.pop();
}
parent.right = curr_node;
}
stack.push(curr_node);
}
return root;
}
use of com.pkumar7.trees.RootingTree.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.trees.RootingTree.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.trees.RootingTree.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.trees.RootingTree.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;
}
Aggregations