Search in sources :

Example 11 with TreeNode

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

the class NovemberW3 method morrisTraversal.

/* https://leetcode.com/problems/binary-tree-inorder-traversal/*/
public void morrisTraversal(TreeNode root) {
    TreeNode curr = 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
                curr.right = null;
                System.out.println(root.val);
                root = root.right;
            } else {
                // Construct the threading
                curr.right = root;
                root = root.left;
            }
        } else {
            System.out.println(root.val);
            root = root.right;
        }
    }
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 12 with TreeNode

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

the class NovemberW3 method constructRec.

public TreeNode constructRec(int[] nums, int left, int right) {
    if (left >= right) {
        return null;
    }
    int max = Integer.MIN_VALUE;
    int max_idx = -1;
    for (int idx = left; idx < right; idx++) {
        if (nums[idx] > max) {
            max = nums[idx];
            max_idx = idx;
        }
    }
    TreeNode root = new TreeNode(max);
    root.left = constructRec(nums, left, max_idx - 1);
    root.right = constructRec(nums, max_idx + 1, right);
    return root;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 13 with TreeNode

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

the class NovemberW3 method constructUsingStack.

public TreeNode constructUsingStack(int[] nums) {
    Deque<TreeNode> stack = new LinkedList<>();
    for (int i = 0; i < nums.length; i++) {
        TreeNode curr = new TreeNode(nums[i]);
        while (!stack.isEmpty() && stack.peek().val < nums[i]) {
            curr.left = stack.pop();
        }
        if (!stack.isEmpty()) {
            stack.peek().right = curr;
        }
        stack.push(curr);
    }
    return stack.isEmpty() ? null : stack.removeLast();
}
Also used : TreeNode(com.pkumar7.TreeNode) LinkedList(java.util.LinkedList)

Example 14 with TreeNode

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

the class NovemberW3 method formBstFromRec.

public TreeNode formBstFromRec(int[] preOrder, int bound) {
    if (idx == preOrder.length || preOrder[idx] > bound) {
        return null;
    }
    TreeNode curr = new TreeNode(preOrder[idx++]);
    curr.left = formBstFromRec(preOrder, curr.val);
    curr.right = formBstFromRec(preOrder, bound);
    return curr;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 15 with TreeNode

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

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