Search in sources :

Example 26 with TreeNode

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

Example 27 with TreeNode

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

the class A method isEvenOddTree.

/*
     * 1609. Even Odd Tree
     * https://leetcode.com/problems/even-odd-tree/
     * */
public boolean isEvenOddTree(TreeNode root) {
    Queue<TreeNode> q = new LinkedList<>();
    q.offer(root);
    int level = 0;
    while (!q.isEmpty()) {
        int size = q.size();
        int prev = 0;
        if (level % 2 == 0) {
            prev = Integer.MIN_VALUE;
        } else {
            prev = Integer.MAX_VALUE;
        }
        while (size-- > 0) {
            TreeNode curr = q.poll();
            int val = curr.val;
            if (level % 2 == 0) {
                if (val % 2 == 0 || prev >= val) {
                    return false;
                }
                prev = val;
            } else {
                if (val % 2 != 0 || prev <= val) {
                    return false;
                }
                prev = val;
            }
            if (curr.left != null) {
                q.offer(curr.left);
            }
            if (curr.right != null) {
                q.offer(curr.right);
            }
        }
        level++;
    }
    return true;
}
Also used : TreeNode(com.pkumar7.TreeNode) LinkedList(java.util.LinkedList)

Example 28 with TreeNode

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

the class JulyWeek4 method lowestCommonAncestor.

/**
 * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
 * *
 */
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    // recurseTree(root, p, q);
    // return result;
    TreeNode lca = recurseTreeAnother(root, p, q);
    int d1 = getShortestDistance(lca, p, 0);
    int d2 = getShortestDistance(lca, q, 0);
    int totalDistance = d1 + d2;
    System.out.println("totalDistance: " + totalDistance);
    return lca;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 29 with TreeNode

use of com.pkumar7.TreeNode in project interview by dqnn.

the class _102BinaryTreeLevelOrderTraversal method levelOrder.

/**
 * 102. Binary Tree Level Order Traversal
 * Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
 *
 *     For example:
 *     Given binary tree [3,9,20,null,null,15,7],
 *          3
 *         / \
 *        9  20
 *      /  \
 *     15   7
 *     [
 *     [3],
 *     [9,20],
 *     [15,7]
 *     ]
 *     time : O(n);
 *     space : O(n);
 * @param root
 * @return
 */
public static List<List<Integer>> levelOrder(TreeNode root) {
    List<List<Integer>> res = new ArrayList<>();
    if (root == null)
        return res;
    Queue<TreeNode> queue = new LinkedList<>();
    queue.offer(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            TreeNode cur = queue.poll();
            if (cur.left != null)
                queue.offer(cur.left);
            if (cur.right != null)
                queue.offer(cur.right);
            list.add(cur.val);
        }
        res.add(list);
    }
    return res;
}
Also used : TreeNode(hatecode._0001_0999.TreeNode) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 30 with TreeNode

use of com.pkumar7.TreeNode in project interview by dqnn.

the class _1038BinarySearchTreeToGreaterSumTree method convertBST_Moris.

public TreeNode convertBST_Moris(TreeNode root) {
    TreeNode cur = root;
    int sum = 0;
    while (cur != null) {
        if (cur.right != null) {
            // traverse right subtree.
            TreeNode leftMost = cur.right;
            while (leftMost.left != null && leftMost.left != cur) {
                // locate the left-most node of cur's right subtree.
                leftMost = leftMost.left;
            }
            if (leftMost.left == null) {
                // never visit the left-most node yet.
                // construct a way back to cur.
                leftMost.left = cur;
                // explore right.
                cur = cur.right;
            } else {
                // visited leftMost already, which implies now on way back.
                // cut off the fabricated link.
                leftMost.left = null;
                // update sum.
                sum += cur.val;
                // update node value.
                cur.val = sum;
                // continue on way back.
                cur = cur.left;
            }
        } else {
            // no right child: 1) cur is the right-most of unvisited nodes; 2) must traverse left.
            // update sum.
            sum += cur.val;
            // update node value.
            cur.val = sum;
            // continue on way back.
            cur = cur.left;
        }
    }
    return root;
}
Also used : TreeNode(hatecode._0001_0999.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