use of com.pkumar7.TreeNode in project gdmatrix by gdmatrix.
the class TypeTreeNode method findOldTypeTreeNode.
private TypeTreeNode findOldTypeTreeNode(String derivedTypeId) {
TypeTreeNode node = null;
if (children != null) {
int i = 0;
while (node == null && i < children.size()) {
TreeNode currentNode = children.get(i);
if (currentNode instanceof TypeTreeNode) {
String currentTypeId = ((TypeTreeNode) currentNode).getTypeId();
if (derivedTypeId.equals(currentTypeId)) {
node = (TypeTreeNode) currentNode;
}
}
i++;
}
}
return node;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class A method subtreeWithAllDeepest.
/* 865. Smallest Subtree with all the Deepest Nodes
* https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
* */
public TreeNode subtreeWithAllDeepest(TreeNode root) {
ArrayDeque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
ArrayDeque<TreeNode> lastLevel = new ArrayDeque<>();
while (!queue.isEmpty()) {
int size = queue.size();
lastLevel.clear();
lastLevel.addAll(queue);
while (size-- > 0) {
TreeNode curr = queue.poll();
if (curr.left != null)
queue.offer(curr.left);
if (curr.right != null)
queue.offer(curr.right);
}
}
if (lastLevel.size() == 1)
return lastLevel.peek();
return findLowestCommonAncestor(root, lastLevel.peekFirst(), lastLevel.peekLast());
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class A method canMerge.
/* 1932. Merge BSTs to Create Single BST
* https://leetcode.com/problems/merge-bsts-to-create-single-bst/
* */
public TreeNode canMerge(List<TreeNode> trees) {
HashMap<Integer, TreeNode> map = new HashMap<>();
Set<TreeNode> deletedSet = new HashSet<>();
HashMap<TreeNode, TreeNode> parentMap = new HashMap<>();
for (TreeNode t : trees) {
map.putIfAbsent(t.val, t);
}
for (TreeNode root : trees) {
if (root.left != null && map.containsKey(root.left.val) && !deletedSet.contains(map.get(root.left.val))) {
TreeNode parent = parentMap.getOrDefault(root, null);
if (parent != map.get(root.left.val)) {
root.left = map.get(root.left.val);
parentMap.put(map.get(root.left.val), root);
deletedSet.add(map.get(root.left.val));
}
}
if (root.right != null && map.containsKey(root.right.val) && !deletedSet.contains(map.get(root.right.val))) {
TreeNode parent = parentMap.getOrDefault(root, null);
if (parent != map.get(root.right.val)) {
root.right = map.get(root.right.val);
parentMap.put(map.get(root.right.val), root);
deletedSet.add(map.get(root.right.val));
}
}
}
if (deletedSet.size() != trees.size() - 1) {
return null;
}
for (TreeNode r : trees) {
if (!deletedSet.contains(r)) {
if (isValidBST(r)) {
return r;
}
}
}
return null;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class A method findLowestCommonAncestor.
public TreeNode findLowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2) {
if (root == null)
return null;
if (root.val == node1.val)
return root;
if (root.val == node2.val)
return root;
TreeNode left = findLowestCommonAncestor(root.left, node1, node2);
TreeNode right = findLowestCommonAncestor(root.right, node1, node2);
if (left == null && right == null)
return null;
if (left != null && right == null)
return left;
if (left == null && right != null)
return right;
if (left != null && right != null)
return root;
return null;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class A method increasingBSTRec.
public TreeNode increasingBSTRec(TreeNode root, TreeNode tail) {
if (root == null)
return tail;
TreeNode r_left = increasingBSTRec(root.left, root);
root.left = null;
TreeNode r_right = increasingBSTRec(root.right, tail);
root.right = r_right;
return r_left;
}
Aggregations