Search in sources :

Example 36 with TreeNode

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;
}
Also used : TreeNode(org.apache.myfaces.custom.tree2.TreeNode)

Example 37 with TreeNode

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

Example 38 with TreeNode

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

Example 39 with TreeNode

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

Example 40 with TreeNode

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

Aggregations

TreeNode (com.pkumar7.TreeNode)47 LinkedList (java.util.LinkedList)16 ArrayList (java.util.ArrayList)13 TreeNode (hatecode._0001_0999.TreeNode)11 Stack (java.util.Stack)7 TreeNode (org.apache.myfaces.custom.tree2.TreeNode)6 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)3 List (java.util.List)3 TreeNodeBase (org.apache.myfaces.custom.tree2.TreeNodeBase)3 Account (com.autentia.tnt.businessobject.Account)1 AccountEntry (com.autentia.tnt.businessobject.AccountEntry)1 AccountEntryType (com.autentia.tnt.businessobject.AccountEntryType)1 Activity (com.autentia.tnt.businessobject.Activity)1 AdminHoliday (com.autentia.tnt.businessobject.AdminHoliday)1 Bill (com.autentia.tnt.businessobject.Bill)1 Book (com.autentia.tnt.businessobject.Book)1 BulletinBoard (com.autentia.tnt.businessobject.BulletinBoard)1 BulletinBoardCategory (com.autentia.tnt.businessobject.BulletinBoardCategory)1 CompanyState (com.autentia.tnt.businessobject.CompanyState)1