use of com.pkumar7.trees.RootingTree.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class A method isEvenOddTree.
/*
* 1609. Even Odd Tree
* https://leetcode.com/problems/even-odd-tree/
* */
public boolean isEvenOddTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int level = 0;
while (!q.isEmpty()) {
int size = q.size();
int prev = 0;
if (level % 2 == 0) {
prev = Integer.MIN_VALUE;
} else {
prev = Integer.MAX_VALUE;
}
while (size-- > 0) {
TreeNode curr = q.poll();
int val = curr.val;
if (level % 2 == 0) {
if (val % 2 == 0 || prev >= val) {
return false;
}
prev = val;
} else {
if (val % 2 != 0 || prev <= val) {
return false;
}
prev = val;
}
if (curr.left != null) {
q.offer(curr.left);
}
if (curr.right != null) {
q.offer(curr.right);
}
}
level++;
}
return true;
}
use of com.pkumar7.trees.RootingTree.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class JulyWeek4 method lowestCommonAncestor.
/**
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
* *
*/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// recurseTree(root, p, q);
// return result;
TreeNode lca = recurseTreeAnother(root, p, q);
int d1 = getShortestDistance(lca, p, 0);
int d2 = getShortestDistance(lca, q, 0);
int totalDistance = d1 + d2;
System.out.println("totalDistance: " + totalDistance);
return lca;
}
use of com.pkumar7.trees.RootingTree.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.trees.RootingTree.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.trees.RootingTree.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;
}
Aggregations