Search in sources :

Example 41 with TreeNode

use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.

the class A method widthOfBinaryTree.

/*
    * 662. Maximum Width of Binary Tree
    * https://leetcode.com/problems/maximum-width-of-binary-tree/
    */
public int widthOfBinaryTree(TreeNode root) {
    if (root == null)
        return 0;
    Queue<TreeNode> q = new LinkedList<>();
    Map<TreeNode, Integer> map = new HashMap<>();
    map.put(root, 1);
    q.offer(root);
    int maxWidth = 0;
    while (!q.isEmpty()) {
        int size = q.size();
        int start = 0;
        int end = 0;
        for (int i = 0; i < size; i++) {
            TreeNode curr = q.poll();
            if (i == 0)
                start = map.get(curr);
            if (i == size - 1)
                end = map.get(curr);
            if (curr.left != null) {
                q.offer(curr.left);
                map.put(curr.left, 2 * map.get(curr));
            }
            if (curr.right != null) {
                q.offer(curr.right);
                map.put(curr.right, 2 * map.get(curr) + 1);
            }
        }
        int currMax = end - start + 1;
        maxWidth = Math.max(currMax, maxWidth);
    }
    return maxWidth;
}
Also used : HashMap(java.util.HashMap) TreeNode(com.pkumar7.TreeNode) LinkedList(java.util.LinkedList)

Example 42 with TreeNode

use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.

the class B method lowestCommonAncestor.

/* 1676. Lowest Common Ancestor of a Binary Tree IV
    * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iv/
    * */
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
    if (root == null)
        return null;
    for (TreeNode n : nodes) {
        if (root == n) {
            return root;
        }
    }
    TreeNode l = lowestCommonAncestor(root.left, nodes);
    TreeNode r = lowestCommonAncestor(root.right, nodes);
    if (l != null && r == null)
        return l;
    if (l == null && r != null)
        return r;
    if (l != null && r != null)
        return root;
    return null;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 43 with TreeNode

use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.

the class Isomorphism method isIsomorphic.

/*
    * AHU (Aho, Hopcroft, Ullman) algorithm uses clever serialization technique
    * for representing a tree as a unique string.
    * 1) Determine tree isomorphism in time O(|V|^2).
    * 2) Uses complete history of degree spectrum of the vertex descendants as a complete
    *    invariant.
    * */
public boolean isIsomorphic(LinkedList<Integer>[] graph1, LinkedList<Integer>[] graph2) {
    TreeCenter centers1 = new TreeCenter();
    List<Integer> a = centers1.findCenter(graph1);
    List<Integer> b = centers1.findCenter(graph2);
    System.out.println("Center for a " + a.get(0) + "\nCenter for b " + b.get(0));
    RootingTree tree = new RootingTree();
    TreeNode rootNode = tree.rootTree(graph1, a.get(0), null);
    String tree1_encode = encode(rootNode);
    System.out.println("tree1_encode = " + tree1_encode);
    for (int centers : b) {
        TreeNode root = tree.rootTree(graph2, centers, null);
        String tree2_encode = encode(root);
        System.out.println("tree2_encode = " + tree2_encode);
        if (tree1_encode.equals(tree2_encode)) {
            return true;
        }
    }
    return false;
}
Also used : TreeNode(com.pkumar7.trees.RootingTree.TreeNode)

Example 44 with TreeNode

use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.

the class NovemberW3 method printSpiral.

/* Tree traversal spiral order using Stack*/
void printSpiral(TreeNode TreeNode) {
    // Your code here
    Stack<TreeNode> s1 = new Stack<>();
    Stack<TreeNode> s2 = new Stack<>();
    s1.push(TreeNode);
    List<Integer> list = new ArrayList<>();
    while (!s1.isEmpty() || !s2.isEmpty()) {
        while (!s1.isEmpty()) {
            TreeNode curr = s1.pop();
            list.add(curr.val);
            if (curr.right != null) {
                s2.push(curr.right);
            }
            if (curr.left != null) {
                s2.push(curr.left);
            }
        }
        while (!s2.isEmpty()) {
            TreeNode curr = s2.pop();
            list.add(curr.val);
            if (curr.left != null) {
                s1.push(curr.left);
            }
            if (curr.right != null) {
                s1.push(curr.right);
            }
        }
    }
    for (int ele : list) {
        System.out.print(ele + " ");
    }
}
Also used : TreeNode(com.pkumar7.TreeNode) ArrayList(java.util.ArrayList) Stack(java.util.Stack)

Example 45 with TreeNode

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

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