Search in sources :

Example 31 with TreeNode

use of com.pkumar7.trees.RootingTree.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)

Example 32 with TreeNode

use of com.pkumar7.trees.RootingTree.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 33 with TreeNode

use of com.pkumar7.trees.RootingTree.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 34 with TreeNode

use of com.pkumar7.trees.RootingTree.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 35 with TreeNode

use of com.pkumar7.trees.RootingTree.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)

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