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;
}
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;
}
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;
}
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;
}
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();
}
}
Aggregations