Search in sources :

Example 16 with TreeNode

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

the class NovemberW3 method fixSwappedNodes.

public void fixSwappedNodes(TreeNode root) {
    TreeNode curr = null, prev = null, first = null, second = null;
    while (root != null) {
        if (root.left != null) {
            // Connect threadings for root
            curr = root.left;
            while (curr.right != null && curr.right != root) {
                curr = curr.right;
            }
            if (curr.right != null) {
                // Threading already established
                if (prev != null && prev.val > root.val) {
                    if (first == null) {
                        first = prev;
                    }
                    second = root;
                }
                prev = root;
                curr.right = null;
                // System.out.println(curr.val);
                root = root.right;
            } else {
                // Construct the threading
                curr.right = root;
                root = root.left;
            }
        } else {
            if (prev != null && prev.val > root.val) {
                if (first == null) {
                    first = prev;
                }
                second = root;
            }
            // System.out.println(curr.val);
            prev = root;
            root = root.right;
        }
    }
    if (first != null && second != null) {
        int t = first.val;
        first.val = second.val;
        second.val = t;
    }
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 17 with TreeNode

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

the class NovemberW4 method largestValues.

/*
        https://leetcode.com/problems/find-largest-value-in-each-tree-row/
    */
public List<Integer> largestValues(TreeNode root) {
    Queue<TreeNode> q = new LinkedList<>();
    q.offer(root);
    List<Integer> result = new ArrayList<>();
    while (!q.isEmpty()) {
        int size = q.size();
        int max = -1;
        while (size-- > 0) {
            TreeNode curr = q.poll();
            max = Math.max(curr.val, max);
            if (curr.left != null) {
                q.offer(curr.left);
            }
            if (curr.right != null) {
                q.offer(curr.right);
            }
        }
        result.add(max);
    }
    return result;
}
Also used : TreeNode(com.pkumar7.TreeNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 18 with TreeNode

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

the class OctoberW1 method findBottomLeftValueBFS.

/**
 * https://leetcode.com/problems/find-bottom-left-tree-value/
 * 513. Find Bottom Left Tree Value
 *
 *     2
 *    / \
 *   1   3
 *
 *         1
 *        / \
 *       2   3
 *      /   / \
 *     4   5   6
 *        /
 *       7
 */
public int findBottomLeftValueBFS(TreeNode root) {
    Queue<TreeNode> mQueue = new LinkedList<>();
    mQueue.offer(root);
    int lastNode = 0;
    while (!mQueue.isEmpty()) {
        int size = mQueue.size();
        for (int i = 0; i < size; i++) {
            TreeNode curr = mQueue.poll();
            if (curr.left != null) {
                mQueue.offer(curr.left);
            }
            if (curr.right != null) {
                mQueue.offer(curr.right);
            }
            if (i == 0) {
                lastNode = curr.val;
            }
        }
    }
    return lastNode;
}
Also used : TreeNode(com.pkumar7.TreeNode) LinkedList(java.util.LinkedList)

Example 19 with TreeNode

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

the class MayW2 method getTargetCopy.

/* https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/ */
public final TreeNode getTargetCopy(final TreeNode o, final TreeNode c, final TreeNode t) {
    if (o == null && c == null)
        return null;
    if (t.val == c.val) {
        return c;
    }
    TreeNode resLeft = getTargetCopy(o.left, c.left, t);
    TreeNode resRight = getTargetCopy(o.right, c.right, t);
    if (resLeft != null)
        return resLeft;
    return resRight;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 20 with TreeNode

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

the class JanuaryW3 method helper.

public TreeNode helper(StringBuilder builder) {
    if (builder.length() == 0) {
        return null;
    } else if (builder.length() <= 2) {
        int root_val = 0;
        int n = builder.length();
        int i = builder.charAt(0) == '-' ? 1 : 0;
        while (i < n && builder.charAt(i) != '(') {
            root_val = root_val * 10 + builder.charAt(i) - '0';
            i++;
        }
        root_val = builder.charAt(0) == '-' ? -root_val : root_val;
        return new TreeNode(root_val);
    }
    int root_val = 0;
    int n = builder.length();
    int i = builder.charAt(0) == '-' ? 1 : 0;
    while (i < n && builder.charAt(i) != '(') {
        root_val = root_val * 10 + builder.charAt(i) - '0';
        i++;
    }
    root_val = builder.charAt(0) == '-' ? -root_val : root_val;
    builder.delete(0, i);
    Stack<Character> stack = new Stack<>();
    int start = 0;
    int end = 0;
    if (end < builder.length()) {
        stack.push(builder.charAt(end++));
        while (!stack.isEmpty() && builder.length() > 0) {
            char curr = builder.charAt(end++);
            if (curr == '(') {
                stack.push(curr);
            } else if (curr == ')') {
                stack.pop();
            }
        }
    }
    StringBuilder left_subs;
    if (start == end) {
        left_subs = new StringBuilder();
    } else {
        left_subs = new StringBuilder(builder.substring(start + 1, end - 1));
    }
    start = end;
    if (end < builder.length()) {
        stack.push(builder.charAt(end++));
        while (!stack.isEmpty() && builder.length() > 0) {
            char curr = builder.charAt(end++);
            if (curr == '(') {
                stack.push(curr);
            } else if (curr == ')') {
                stack.pop();
            }
        }
    }
    StringBuilder right_sub;
    if (start == end) {
        right_sub = new StringBuilder();
    } else {
        right_sub = new StringBuilder(builder.substring(start + 1, end - 1));
    }
    TreeNode curr = new TreeNode(root_val);
    curr.left = helper(left_subs);
    curr.right = helper(right_sub);
    return curr;
}
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