use of com.pkumar7.TreeNode in project TNTConcept by autentia.
the class MenuBean method openNode.
/**
* Add non-leaf node to current node if it is accesible by current user
* @param path path of current node
* @param creds current user
* @param neededRole role needed to render the node
* @param parent parent node
* @param cmd command name
* @return true if node has been created
*/
private boolean openNode(Stack<TreeNode> path, Principal creds, GrantedAuthority neededRole, String cmd) {
boolean added = false;
if (neededRole == null || creds.hasAuthority(neededRole)) {
String text;
try {
text = msg.getString("menu." + cmd);
} catch (MissingResourceException e) {
text = "MISSING : " + cmd + " : MISSING";
}
TreeNode child = new TreeNodeBase("folder", text, cmd, false);
path.peek().getChildren().add(child);
path.push(child);
added = true;
}
log.debug("openNode - " + (added ? "OPEN " : "IGNORE") + ": " + cmd);
return added;
}
use of com.pkumar7.TreeNode in project interview by dqnn.
the class _1028RecoverATreeFromPreorderTraversal method recoverFromPreorder_Best.
/*
1028. Recover a Tree From Preorder Traversal
We run a preorder depth first search on the root of a binary tree.
At each node in this traversal, we output D dashes (where D is
the depth of this node), then we output the value of this node.
(If the depth of a node is D, the depth of its immediate child
is D+1. The depth of the root node is 0.)
If a node has only one child, that child is guaranteed to be
the left child.
Given the output S of this traversal, recover the tree and
return its root.
Input: "1-2--3---4-5--6---7"
Output: [1,2,5,3,null,6,null,4,null,7]
*/
// thinking process: O(n)/O(n) n = s.length()
// given a string, preorder visit this tree, and we form a dash as level,
// level 1 is 1 dash, level 2 is two dashes, if there is only node, that node is
// guaranteed as left child, return the root of the tree
// the difficulty of this problem is we have 2 dimensions problems,
// the sequence is only pre-ordered, and we have dashes to show which level it is
// so we need a structure to store levels where we are
// so we use a stack to store visited node,
public TreeNode recoverFromPreorder_Best(String s) {
if (s == null || s.length() < 1)
return null;
int level = 0, var = 0;
Stack<TreeNode> stack = new Stack<>();
for (int i = 0; i < s.length(); ) {
for (level = 0; s.charAt(i) == '-'; i++) level++;
for (var = 0; i < s.length() && s.charAt(i) != '-'; i++) {
var = var * 10 + (s.charAt(i) - '0');
}
//
while (stack.size() > level) {
stack.pop();
}
TreeNode node = new TreeNode(var);
if (!stack.isEmpty()) {
if (stack.peek().left == null) {
stack.peek().left = node;
} else {
stack.peek().right = node;
}
}
stack.add(node);
}
while (stack.size() > 1) {
stack.pop();
}
return stack.pop();
}
use of com.pkumar7.TreeNode in project interview by dqnn.
the class _107BinaryTreeLevelOrderTraversalII method levelOrderBottom3.
// reverse the result
public List<List<Integer>> levelOrderBottom3(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 len = queue.size();
for (int i = 0; i < len; 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);
}
Collections.reverse(res);
return res;
}
use of com.pkumar7.TreeNode in project interview by dqnn.
the class _1008ConstructBinarySearchTreeFromPreorderTraversal method bstFromPreorder_iterative.
public TreeNode bstFromPreorder_iterative(int[] preorder) {
if (preorder == null || preorder.length == 0) {
return null;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode root = new TreeNode(preorder[0]);
stack.push(root);
for (int i = 1; i < preorder.length; i++) {
TreeNode node = new TreeNode(preorder[i]);
if (preorder[i] < stack.peek().val) {
stack.peek().left = node;
} else {
TreeNode parent = stack.peek();
while (!stack.isEmpty() && preorder[i] > stack.peek().val) {
parent = stack.pop();
}
parent.right = node;
}
stack.push(node);
}
return root;
}
use of com.pkumar7.TreeNode in project interview by dqnn.
the class _1008ConstructBinarySearchTreeFromPreorderTraversal method helper.
private TreeNode helper(int[] A, int max) {
if (idx == A.length || A[idx] > max)
return null;
TreeNode root = new TreeNode(A[idx++]);
root.left = helper(A, root.val);
root.right = helper(A, max);
return root;
}
Aggregations