Search in sources :

Example 31 with TreeNode

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

the class _1038BinarySearchTreeToGreaterSumTree method bstToGst_Iterative.

public TreeNode bstToGst_Iterative(TreeNode root) {
    int sum = 0;
    TreeNode cur = root;
    Deque<TreeNode> stack = new ArrayDeque<>();
    while (!stack.isEmpty() || cur != null) {
        while (cur != null) {
            stack.push(cur);
            cur = cur.right;
        }
        cur = stack.pop();
        cur.val += sum;
        sum = cur.val;
        cur = cur.left;
    }
    return root;
}
Also used : TreeNode(hatecode._0001_0999.TreeNode)

Example 32 with TreeNode

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

the class _1028RecoverATreeFromPreorderTraversal method helper.

public TreeNode helper(String s, int depth) {
    int numDash = 0;
    while (index + numDash < s.length() && s.charAt(index + numDash) == '-') {
        numDash++;
    }
    if (numDash != depth)
        return null;
    int next = index + numDash;
    while (next < s.length() && s.charAt(next) != '-') next++;
    int val = Integer.parseInt(s.substring(index + numDash, next));
    index = next;
    TreeNode root = new TreeNode(val);
    root.left = helper(s, depth + 1);
    root.right = helper(s, depth + 1);
    return root;
}
Also used : TreeNode(hatecode._0001_0999.TreeNode)

Example 33 with TreeNode

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

the class _102BinaryTreeLevelOrderTraversal method levelOrder3.

// my owned solutions
public List<List<Integer>> levelOrder3(TreeNode root) {
    List<List<Integer>> res = new ArrayList<>();
    if (null == root) {
        return res;
    }
    Queue<TreeNode> queue = new LinkedList<>();
    queue.offer(root);
    while (!queue.isEmpty()) {
        List<Integer> list = new ArrayList<>();
        int n = queue.size();
        for (int i = 0; i < n; i++) {
            TreeNode node = queue.poll();
            list.add(node.val);
            if (null != node.left) {
                queue.offer(node.left);
            }
            if (null != node.right) {
                queue.offer(node.right);
            }
        }
        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 34 with TreeNode

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

the class _107BinaryTreeLevelOrderTraversalII method levelOrderBottom.

/**
 *     For example:
 *     Given binary tree [3,9,20,null,null,15,7],
 *          3
 *         / \
 *        9  20
 *      /  \
 *     15   7
 *     [
 *     [15,7],
 *     [9,20],
 *     [3]
 *     ]
 *
 *     time : O(n)
 *     space : O(n)
 *
 * @param root
 * @return
 */
public static List<List<Integer>> levelOrderBottom(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);
        }
        // Inserts the specified element at the specified position in this list.
        // Shifts the element currently at that position (if any) and any subsequent
        // elements to the right (adds one to their indices).
        // like offer()
        res.add(0, 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 35 with TreeNode

use of com.pkumar7.TreeNode in project gdmatrix by gdmatrix.

the class DocumentTreeBean method getTreeData.

public TreeModel getTreeData() {
    try {
        MenuItemCursor mic = UserSessionBean.getCurrentInstance().getMenuModel().getSelectedMenuItem();
        String propertyName = (String) mic.getProperties().get(FILTERPROPERTY_NAME_PROPERTY);
        if (propertyName == null) {
            propertyName = (String) mic.getProperties().get(TREE_FILTERPROPERTY_NAME_PROPERTY);
        }
        String propertyValue = (String) mic.getProperties().get(FILTERPROPERTY_VALUE_PROPERTY);
        if (propertyValue == null) {
            propertyValue = (String) mic.getProperties().get(TREE_FILTERPROPERTY_VALUE_PROPERTY);
        }
        if (propertyName == null)
            return getEmptyModel();
        CachedDocumentManagerClient client = getDocumentManagerClient();
        DocumentFilter filter = new DocumentFilter();
        Property p = new Property();
        p.setName(propertyName);
        p.getValue().add(propertyValue);
        filter.getProperty().add(p);
        filter.setVersion(0);
        OrderByProperty ob = new OrderByProperty();
        ob.setName("title");
        filter.getOrderByProperty().add(ob);
        filter.setIncludeContentMetadata(true);
        List<Document> documents = client.findDocuments(filter);
        // make up table
        Table table2 = new Table(new String[] { "path", "uuid", "size", "icon", "filename" });
        for (Document document : documents) {
            String path = document.getTitle();
            Content content = document.getContent();
            String size = "";
            Number nsize = content.getSize();
            if (nsize != null)
                size = DocumentUtils.getSizeString(nsize.longValue());
            String uuid = content.getContentId();
            String mimeType = content.getContentType();
            String icon = DocumentBean.getContentTypeIcon(mimeType);
            String title = "document";
            int index = path.lastIndexOf("\\");
            if (index == -1)
                title = path;
            else
                title = path.substring(index + 1);
            String extension = MimeTypeMap.getMimeTypeMap().getExtension(mimeType);
            String filename = DocumentUtils.getFilename(title) + "." + extension;
            table2.addRow(new Object[] { path, uuid, size, icon, filename });
        }
        // create tree model
        TreeNode root = TreeModelUtils.createTree(table2, "\\\\", "path");
        TreeModelBase treeModel = new TreeModelBase(root);
        TreeStateBase state = new TreeStateBase();
        // expand root
        state.toggleExpanded("0");
        // expand one level
        int count = root.getChildren().size();
        for (int i = 0; i < count; i++) {
            state.toggleExpanded("0:" + i);
        }
        treeModel.setTreeState(state);
        return treeModel;
    } catch (Exception ex) {
        ex.printStackTrace();
        return getEmptyModel();
    }
}
Also used : Table(org.santfeliu.util.Table) TreeModelBase(org.apache.myfaces.custom.tree2.TreeModelBase) MenuItemCursor(org.santfeliu.faces.menu.model.MenuItemCursor) TreeStateBase(org.apache.myfaces.custom.tree2.TreeStateBase) DocumentFilter(org.matrix.doc.DocumentFilter) Document(org.matrix.doc.Document) Content(org.matrix.doc.Content) ExtendedTreeNode(org.santfeliu.faces.tree.ExtendedTreeNode) TreeNode(org.apache.myfaces.custom.tree2.TreeNode) CachedDocumentManagerClient(org.santfeliu.doc.client.CachedDocumentManagerClient) Property(org.matrix.dic.Property) CMSProperty(org.santfeliu.web.bean.CMSProperty) OrderByProperty(org.matrix.doc.OrderByProperty) OrderByProperty(org.matrix.doc.OrderByProperty)

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