Search in sources :

Example 16 with TreeNode

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

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

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

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

Example 20 with TreeNode

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

the class JulyW1 method createBST.

TreeNode createBST(List<Integer> nums, int start, int end) {
    if (start > end) {
        return null;
    }
    int mid = (start + end) / 2;
    TreeNode root = new TreeNode(nums.get(mid));
    root.left = createBST(nums, start, mid - 1);
    root.right = createBST(nums, mid + 1, end);
    return root;
}
Also used : TreeNode(com.pkumar7.TreeNode)

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