use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class B method findLCA.
public TreeNode findLCA(TreeNode root, TreeNode p, TreeNode q) {
if (root == null)
return root;
if (root == p || root == q) {
dfs(root);
return root;
}
sets.add(root.val);
TreeNode left = findLCA(root.left, p, q);
TreeNode right = findLCA(root.right, p, q);
if (left != null && right != null) {
return root;
}
if (left != null && right == null) {
return left;
}
if (right != null && left == null) {
return right;
}
return null;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class Isomorphism method encode.
public String encode(TreeNode root) {
if (root == null)
return "";
List<String> labels = new ArrayList<>();
for (TreeNode adj : root.children) {
labels.add(encode(adj));
}
Collections.sort(labels);
StringBuilder out = new StringBuilder();
for (String child : labels) {
out.append(child);
}
// Knuth Tuple
return "(" + out.toString() + ")";
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class MinTimeToBurnAllNodes method main.
public static void main(String[] args) throws Exception {
/*
* Minimum time to burn all nodes of the tree
* */
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int elementsCount = Integer.parseInt(bufferedReader.readLine().trim());
HashMap<Integer, TreeNode> map = new HashMap<>();
TreeNode root = null;
for (int i = 1; i <= elementsCount - 1; i++) {
String pair = bufferedReader.readLine();
int u = Integer.parseInt(pair.split(" ")[0]);
int v = Integer.parseInt(pair.split(" ")[1]);
if (i == 1) {
root = new TreeNode(u);
root.left = new TreeNode(v);
map.put(u, root);
map.put(v, root.left);
} else {
TreeNode node = map.getOrDefault(u, null);
TreeNode newNode = new TreeNode(v);
if (node != null) {
if (node.left == null) {
node.left = newNode;
} else if (node.right == null) {
node.right = newNode;
}
map.put(v, newNode);
}
}
}
int start = Integer.parseInt(bufferedReader.readLine().trim());
HashMap<TreeNode, TreeNode> parentMap = new HashMap<>();
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
TreeNode curr = q.poll();
if (curr.left != null) {
parentMap.put(curr.left, curr);
q.offer(curr.left);
}
if (curr.right != null) {
parentMap.put(curr.right, curr);
q.offer(curr.right);
}
}
Queue<TreeNode> burningQ = new LinkedList<>();
TreeNode startNode = map.get(start);
burningQ.offer(startNode);
HashMap<TreeNode, Integer> visited = new HashMap<>();
visited.put(startNode, 1);
int minimumTime = 0;
while (!burningQ.isEmpty()) {
int size = burningQ.size();
int currTime = 0;
while (size-- > 0) {
TreeNode curr = burningQ.poll();
if (curr.left != null && visited.get(curr.left) == null) {
visited.put(curr.left, 1);
burningQ.offer(curr.left);
currTime = 1;
}
if (curr.right != null && visited.get(curr.right) == null) {
visited.put(curr.right, 1);
burningQ.offer(curr.right);
currTime = 1;
}
if (parentMap.get(curr) != null && visited.get(parentMap.get(curr)) == null) {
visited.put(parentMap.get(curr), 1);
burningQ.offer(parentMap.get(curr));
currTime = 1;
}
}
if (currTime == 1) {
minimumTime += 1;
}
}
// out.write("" + minimumTime);
System.out.println(minimumTime);
bufferedReader.close();
out.flush();
out.close();
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class SeptemberW1 method convert.
/**
* https://www.geeksforgeeks.org/convert-ternary-expression-binary-tree/
* https://leetcode.com/discuss/interview-question/124826/ternary-expression-to-binary-tree
*
* Input: "a?b:c"
* Output:
* a
* / \
* b c
*
* Input: "a?b?c:d:e"
* Output:
* a
* / \
* b e
* / \
* c d
*
* *
*/
public TreeNode convert(char[] expr) {
if (expr.length == 0) {
return null;
}
TreeNode root = new TreeNode(expr[0]);
Stack<TreeNode> stack = new Stack<>();
for (int i = 1; i < expr.length; i += 2) {
TreeNode node = new TreeNode(expr[i + 1]);
if (expr[i] == '?') {
stack.peek().left = node;
}
if (expr[i] == ':') {
stack.pop();
while (stack.peek().right != null) {
stack.pop();
}
stack.peek().right = node;
}
stack.push(node);
}
return root;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class DecemberW1 method helper.
public TreeNode helper(int[] preorder, int left, int right, HashMap<Integer, Integer> mapInorder) {
if (left > right)
return null;
TreeNode root = new TreeNode(preorder[preIdx++]);
int inIdx = mapInorder.get(root.val);
root.left = helper(preorder, left, inIdx - 1, mapInorder);
root.right = helper(preorder, inIdx + 1, right, mapInorder);
return root;
}
Aggregations