Search in sources :

Example 46 with TreeNode

use of com.pkumar7.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 47 with TreeNode

use of com.pkumar7.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 48 with TreeNode

use of com.pkumar7.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 49 with TreeNode

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

Example 50 with TreeNode

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

the class OctoberW1 method allPossibleFBTMemo.

public List<TreeNode> allPossibleFBTMemo(int N, List<TreeNode>[] memo) {
    List<TreeNode> res = new ArrayList<>();
    if (N == 1) {
        res.add(new TreeNode(0));
        return res;
    }
    if (memo[N] != null) {
        return memo[N];
    }
    for (int leftNum = 1; leftNum <= N - 1; leftNum += 2) {
        List<TreeNode> left = allPossibleFBTMemo(leftNum, memo);
        List<TreeNode> right = allPossibleFBTMemo(N - leftNum - 1, memo);
        for (TreeNode currLeft : left) {
            for (TreeNode currRight : right) {
                TreeNode cur = new TreeNode(0);
                cur.left = currLeft;
                cur.right = currRight;
                res.add(cur);
            }
        }
    }
    memo[N] = res;
    return res;
}
Also used : TreeNode(com.pkumar7.TreeNode) ArrayList(java.util.ArrayList)

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