Search in sources :

Example 51 with TreeNode

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

the class DecemberW2 method postorderTraversal.

/* 
        https://leetcode.com/problems/binary-tree-postorder-traversal/
     */
public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> res = new ArrayList<>();
    res.forEach(ele -> System.out.println(ele));
    Stack<TreeNode> stack = new Stack<>();
    TreeNode lastNodeVisited = null;
    while (!stack.isEmpty() || root != null) {
        if (root != null) {
            stack.push(root);
            root = root.left;
        } else {
            TreeNode top = stack.peek();
            if (top.right != null && lastNodeVisited != top.right) {
                root = top.right;
            } else {
                res.add(top.val);
                lastNodeVisited = stack.pop();
            }
        }
    }
    return res;
}
Also used : TreeNode(com.pkumar7.TreeNode) ArrayList(java.util.ArrayList) Stack(java.util.Stack)

Example 52 with TreeNode

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

the class FebruaryW1 method bstToGstI.

public TreeNode bstToGstI(TreeNode root) {
    sortIncreasing(root);
    TreeNode result = toGst(root);
    return result;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 53 with TreeNode

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

the class FebruaryW2 method insert.

public TreeNode insert(int val, TreeNode node, Integer[] res, int i, int prefixSum) {
    if (node == null) {
        node = new TreeNode(val, 0);
        res[i] = prefixSum;
    } else if (node.val > val) {
        node.sum++;
        node.left = insert(val, node.left, res, i, prefixSum);
    } else if (node.val == val) {
        node.dup++;
        res[i] = prefixSum + node.sum;
    } else {
        node.right = insert(val, node.right, res, i, prefixSum + node.dup + node.sum);
    }
    return node;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 54 with TreeNode

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

the class FebruaryW4 method closestValue.

/* https://leetcode.com/problems/closest-binary-search-tree-value/ */
public int closestValue(TreeNode root, double target) {
    int a = root.val;
    TreeNode kid = root.val < target ? root.right : root.left;
    if (kid == null)
        return a;
    int b = closestValue(kid, target);
    return Math.abs(a - target) < Math.abs(b - target) ? a : b;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 55 with TreeNode

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

the class MarchW4 method helper.

public TreeNode helper(TreeNode root) {
    if (root == null)
        return null;
    if (root.left == null && root.right == null)
        return root;
    TreeNode leftTail = helper(root.left);
    TreeNode rightTail = helper(root.right);
    if (leftTail != null) {
        leftTail.right = root.right;
        root.right = root.left;
        root.left = null;
    }
    return rightTail == null ? leftTail : rightTail;
}
Also used : TreeNode(com.pkumar7.TreeNode)

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