Search in sources :

Example 36 with TreeNode

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

Example 37 with TreeNode

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

the class NovemberW4 method validateBSTUsingStack.

// Tree Inorder Traversal
public boolean validateBSTUsingStack(TreeNode root) {
    TreeNode prev = null;
    if (root == null)
        return true;
    Stack<TreeNode> stack = new Stack<TreeNode>();
    while (root != null || !stack.isEmpty()) {
        while (root != null) {
            stack.push(root);
            root = root.left;
        }
        root = stack.pop();
        if (prev != null && prev.val >= root.val)
            return false;
        prev = root;
        root = root.right;
    }
    return true;
}
Also used : TreeNode(com.pkumar7.TreeNode) Stack(java.util.Stack)

Example 38 with TreeNode

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

the class NovemberW4 method isCousinsBfs.

public boolean isCousinsBfs(TreeNode root, int x, int y) {
    Queue<TreeNode> q = new LinkedList<>();
    q.offer(root);
    TreeNode xParent = null;
    TreeNode yParent = null;
    while (!q.isEmpty()) {
        int size = q.size();
        while (size-- > 0) {
            TreeNode curr = q.poll();
            if (curr.left != null) {
                if (curr.left.val == x) {
                    xParent = curr;
                } else if (curr.left.val == y) {
                    yParent = curr;
                }
                q.offer(curr.left);
            }
            if (curr.right != null) {
                if (curr.right.val == x) {
                    xParent = curr;
                } else if (curr.right.val == y) {
                    yParent = curr;
                }
                q.offer(curr.right);
            }
            if (xParent != null && yParent != null) {
                break;
            }
        }
        if (xParent != null && yParent != null) {
            return xParent != yParent;
        }
        if ((xParent == null && yParent != null) || (xParent != null && yParent == null)) {
            return false;
        }
    }
    return false;
}
Also used : TreeNode(com.pkumar7.TreeNode) LinkedList(java.util.LinkedList)

Example 39 with TreeNode

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

the class NovemberW4 method distanceK.

/* 
        863. All Nodes Distance K in Binary Tree
        https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
        ToDo : DFS
        Important
    */
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
    List<Integer> res = new ArrayList<>();
    if (root == null || K < 0)
        return res;
    buildMap(root, null);
    if (!map.containsKey(target))
        return res;
    Set<TreeNode> visited = new HashSet<>();
    visited.add(target);
    Queue<TreeNode> q = new LinkedList<>();
    q.offer(target);
    while (!q.isEmpty()) {
        if (K == 0) {
            while (!q.isEmpty()) {
                res.add(q.poll().val);
            }
        }
        int size = q.size();
        while (size-- > 0) {
            TreeNode curr = q.poll();
            visited.add(curr);
            List<TreeNode> neighbours = map.get(curr);
            for (TreeNode adj : neighbours) {
                if (visited.contains(adj))
                    continue;
                q.offer(adj);
                visited.add(adj);
            }
        }
        K--;
    }
    return res;
}
Also used : TreeNode(com.pkumar7.TreeNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 40 with TreeNode

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

the class OctoberW1 method allPossibleFBTRec.

/**
 * https://leetcode.com/problems/all-possible-full-binary-trees/
 */
public List<TreeNode> allPossibleFBTRec(int N) {
    List<TreeNode> res = new ArrayList<>();
    if (N == 1) {
        res.add(new TreeNode(0));
        return res;
    }
    for (int leftNum = 1; leftNum <= N - 1; leftNum += 2) {
        List<TreeNode> left = allPossibleFBTRec(leftNum);
        List<TreeNode> right = allPossibleFBTRec(N - leftNum - 1);
        for (TreeNode currLeft : left) {
            for (TreeNode currRight : right) {
                TreeNode cur = new TreeNode(0);
                cur.left = currLeft;
                cur.right = currRight;
                res.add(cur);
            }
        }
    }
    return res;
}
Also used : TreeNode(com.pkumar7.TreeNode) ArrayList(java.util.ArrayList)

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