Search in sources :

Example 21 with TreeNode

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

the class JulyW1 method sortedListToBST.

/* LC : 109. Convert Sorted List to Binary Search Tree
    *  https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
    * https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
    *  Time : O(nlogn)
    */
public TreeNode sortedListToBST(ListNode head) {
    if (head == null)
        return null;
    if (head.next == null)
        return new TreeNode(head.val);
    ListNode mid = null;
    ListNode fast = head, slow = head;
    ListNode left = head, right = head;
    ListNode prevToMid = head;
    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        prevToMid = slow;
        slow = slow.next;
    }
    mid = slow;
    if (mid != head) {
        prevToMid.next = null;
    } else {
        left = null;
    }
    right = mid.next;
    TreeNode root = new TreeNode(mid.val);
    root.left = sortedListToBST(left);
    root.right = sortedListToBST(right);
    return root;
}
Also used : TreeNode(com.pkumar7.TreeNode) ListNode(com.pkumar7.datastructures.ListNode)

Example 22 with TreeNode

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

the class AprilW2 method findClosestLeaf.

/* https://leetcode.com/problems/closest-leaf-in-a-binary-tree/ */
public int findClosestLeaf(TreeNode root, int k) {
    HashMap<TreeNode, ArrayList<TreeNode>> map = new HashMap<>();
    buildGraph(root, null, map);
    TreeNode target = dfs(root, k);
    HashSet<TreeNode> visited = new HashSet<>();
    visited.add(target);
    Queue<TreeNode> q = new LinkedList<>();
    q.offer(target);
    while (!q.isEmpty()) {
        TreeNode curr = q.poll();
        if (curr.left == null && curr.right == null) {
            return curr.val;
        }
        for (TreeNode neig : map.get(curr)) {
            if (visited.contains(neig))
                continue;
            q.offer(neig);
            visited.add(neig);
        }
    }
    return -1;
}
Also used : HashMap(java.util.HashMap) TreeNode(com.pkumar7.TreeNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 23 with TreeNode

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

the class AprilW2 method dfs.

public TreeNode dfs(TreeNode root, int k) {
    if (root == null)
        return null;
    if (root.val == k)
        return root;
    TreeNode left = dfs(root.left, k);
    TreeNode right = dfs(root.right, k);
    if (left != null) {
        return left;
    } else {
        return right;
    }
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 24 with TreeNode

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

the class FebruaryW2 method countSmallerTree.

public List<Integer> countSmallerTree(int[] nums) {
    int n = nums.length;
    Integer[] res = new Integer[n];
    TreeNode root = null;
    for (int i = n - 1; i >= 0; --i) {
        root = insert(nums[i], root, res, i, 0);
    }
    return Arrays.asList(res);
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 25 with TreeNode

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

the class A method rightViewBFS.

public void rightViewBFS(TreeNode root) {
    if (root == null) {
        return;
    }
    Queue<TreeNode> mQueue = new LinkedList<>();
    mQueue.offer(root);
    while (!mQueue.isEmpty()) {
        int size = mQueue.size();
        for (int i = 0; i < size; i++) {
            TreeNode curr = mQueue.poll();
            if (i == size - 1) {
                result.add(curr.val);
            }
            if (curr.left != null) {
                mQueue.offer(curr.left);
            }
            if (curr.right != null) {
                mQueue.offer(curr.right);
            }
        }
    }
}
Also used : TreeNode(com.pkumar7.TreeNode) LinkedList(java.util.LinkedList)

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