Search in sources :

Example 6 with TreeNode

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

the class B method findLCA.

public TreeNode findLCA(TreeNode root, TreeNode p, TreeNode q) {
    if (root == null)
        return root;
    if (root == p || root == q) {
        dfs(root);
        return root;
    }
    sets.add(root.val);
    TreeNode left = findLCA(root.left, p, q);
    TreeNode right = findLCA(root.right, p, q);
    if (left != null && right != null) {
        return root;
    }
    if (left != null && right == null) {
        return left;
    }
    if (right != null && left == null) {
        return right;
    }
    return null;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 7 with TreeNode

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

the class Isomorphism method encode.

public String encode(TreeNode root) {
    if (root == null)
        return "";
    List<String> labels = new ArrayList<>();
    for (TreeNode adj : root.children) {
        labels.add(encode(adj));
    }
    Collections.sort(labels);
    StringBuilder out = new StringBuilder();
    for (String child : labels) {
        out.append(child);
    }
    // Knuth Tuple
    return "(" + out.toString() + ")";
}
Also used : TreeNode(com.pkumar7.trees.RootingTree.TreeNode) ArrayList(java.util.ArrayList)

Example 8 with TreeNode

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

the class MinTimeToBurnAllNodes method main.

public static void main(String[] args) throws Exception {
    /*
         * Minimum time to burn all nodes of the tree
         * */
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(System.out);
    int elementsCount = Integer.parseInt(bufferedReader.readLine().trim());
    HashMap<Integer, TreeNode> map = new HashMap<>();
    TreeNode root = null;
    for (int i = 1; i <= elementsCount - 1; i++) {
        String pair = bufferedReader.readLine();
        int u = Integer.parseInt(pair.split(" ")[0]);
        int v = Integer.parseInt(pair.split(" ")[1]);
        if (i == 1) {
            root = new TreeNode(u);
            root.left = new TreeNode(v);
            map.put(u, root);
            map.put(v, root.left);
        } else {
            TreeNode node = map.getOrDefault(u, null);
            TreeNode newNode = new TreeNode(v);
            if (node != null) {
                if (node.left == null) {
                    node.left = newNode;
                } else if (node.right == null) {
                    node.right = newNode;
                }
                map.put(v, newNode);
            }
        }
    }
    int start = Integer.parseInt(bufferedReader.readLine().trim());
    HashMap<TreeNode, TreeNode> parentMap = new HashMap<>();
    Queue<TreeNode> q = new LinkedList<>();
    q.offer(root);
    while (!q.isEmpty()) {
        TreeNode curr = q.poll();
        if (curr.left != null) {
            parentMap.put(curr.left, curr);
            q.offer(curr.left);
        }
        if (curr.right != null) {
            parentMap.put(curr.right, curr);
            q.offer(curr.right);
        }
    }
    Queue<TreeNode> burningQ = new LinkedList<>();
    TreeNode startNode = map.get(start);
    burningQ.offer(startNode);
    HashMap<TreeNode, Integer> visited = new HashMap<>();
    visited.put(startNode, 1);
    int minimumTime = 0;
    while (!burningQ.isEmpty()) {
        int size = burningQ.size();
        int currTime = 0;
        while (size-- > 0) {
            TreeNode curr = burningQ.poll();
            if (curr.left != null && visited.get(curr.left) == null) {
                visited.put(curr.left, 1);
                burningQ.offer(curr.left);
                currTime = 1;
            }
            if (curr.right != null && visited.get(curr.right) == null) {
                visited.put(curr.right, 1);
                burningQ.offer(curr.right);
                currTime = 1;
            }
            if (parentMap.get(curr) != null && visited.get(parentMap.get(curr)) == null) {
                visited.put(parentMap.get(curr), 1);
                burningQ.offer(parentMap.get(curr));
                currTime = 1;
            }
        }
        if (currTime == 1) {
            minimumTime += 1;
        }
    }
    // out.write("" + minimumTime);
    System.out.println(minimumTime);
    bufferedReader.close();
    out.flush();
    out.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) TreeNode(com.pkumar7.TreeNode) BufferedReader(java.io.BufferedReader) PrintWriter(java.io.PrintWriter)

Example 9 with TreeNode

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

the class SeptemberW1 method convert.

/**
 * https://www.geeksforgeeks.org/convert-ternary-expression-binary-tree/
 * https://leetcode.com/discuss/interview-question/124826/ternary-expression-to-binary-tree
 *
 * Input: "a?b:c"
 * Output:
 *       a
 *      /  \
 *     b    c
 *
 * Input: "a?b?c:d:e"
 * Output:
 *          a
 *         / \
 *        b   e
 *       / \
 *      c   d
 *
 * *
 */
public TreeNode convert(char[] expr) {
    if (expr.length == 0) {
        return null;
    }
    TreeNode root = new TreeNode(expr[0]);
    Stack<TreeNode> stack = new Stack<>();
    for (int i = 1; i < expr.length; i += 2) {
        TreeNode node = new TreeNode(expr[i + 1]);
        if (expr[i] == '?') {
            stack.peek().left = node;
        }
        if (expr[i] == ':') {
            stack.pop();
            while (stack.peek().right != null) {
                stack.pop();
            }
            stack.peek().right = node;
        }
        stack.push(node);
    }
    return root;
}
Also used : TreeNode(com.pkumar7.TreeNode) Stack(java.util.Stack)

Example 10 with TreeNode

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

the class DecemberW1 method helper.

public TreeNode helper(int[] preorder, int left, int right, HashMap<Integer, Integer> mapInorder) {
    if (left > right)
        return null;
    TreeNode root = new TreeNode(preorder[preIdx++]);
    int inIdx = mapInorder.get(root.val);
    root.left = helper(preorder, left, inIdx - 1, mapInorder);
    root.right = helper(preorder, inIdx + 1, right, mapInorder);
    return root;
}
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