Search in sources :

Example 26 with TreeNode

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;
}
Also used : TreeNode(com.pkumar7.TreeNode) LinkedList(java.util.LinkedList)

Example 27 with TreeNode

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;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 28 with TreeNode

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());
}
Also used : TreeNode(com.pkumar7.TreeNode) ArrayDeque(java.util.ArrayDeque)

Example 29 with TreeNode

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;
}
Also used : HashMap(java.util.HashMap) TreeNode(com.pkumar7.TreeNode) HashSet(java.util.HashSet)

Example 30 with TreeNode

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;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Aggregations

TreeNode (com.pkumar7.TreeNode)47 LinkedList (java.util.LinkedList)12 ArrayList (java.util.ArrayList)9 Stack (java.util.Stack)6 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)3 TreeNode (com.pkumar7.trees.RootingTree.TreeNode)2 ListNode (com.pkumar7.datastructures.ListNode)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 ArrayDeque (java.util.ArrayDeque)1 LinkedHashSet (java.util.LinkedHashSet)1