use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class NovemberW3 method fixSwappedNodes.
public void fixSwappedNodes(TreeNode root) {
TreeNode curr = null, prev = null, first = null, second = null;
while (root != null) {
if (root.left != null) {
// Connect threadings for root
curr = root.left;
while (curr.right != null && curr.right != root) {
curr = curr.right;
}
if (curr.right != null) {
// Threading already established
if (prev != null && prev.val > root.val) {
if (first == null) {
first = prev;
}
second = root;
}
prev = root;
curr.right = null;
// System.out.println(curr.val);
root = root.right;
} else {
// Construct the threading
curr.right = root;
root = root.left;
}
} else {
if (prev != null && prev.val > root.val) {
if (first == null) {
first = prev;
}
second = root;
}
// System.out.println(curr.val);
prev = root;
root = root.right;
}
}
if (first != null && second != null) {
int t = first.val;
first.val = second.val;
second.val = t;
}
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class NovemberW4 method largestValues.
/*
https://leetcode.com/problems/find-largest-value-in-each-tree-row/
*/
public List<Integer> largestValues(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
List<Integer> result = new ArrayList<>();
while (!q.isEmpty()) {
int size = q.size();
int max = -1;
while (size-- > 0) {
TreeNode curr = q.poll();
max = Math.max(curr.val, max);
if (curr.left != null) {
q.offer(curr.left);
}
if (curr.right != null) {
q.offer(curr.right);
}
}
result.add(max);
}
return result;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class OctoberW1 method findBottomLeftValueBFS.
/**
* https://leetcode.com/problems/find-bottom-left-tree-value/
* 513. Find Bottom Left Tree Value
*
* 2
* / \
* 1 3
*
* 1
* / \
* 2 3
* / / \
* 4 5 6
* /
* 7
*/
public int findBottomLeftValueBFS(TreeNode root) {
Queue<TreeNode> mQueue = new LinkedList<>();
mQueue.offer(root);
int lastNode = 0;
while (!mQueue.isEmpty()) {
int size = mQueue.size();
for (int i = 0; i < size; i++) {
TreeNode curr = mQueue.poll();
if (curr.left != null) {
mQueue.offer(curr.left);
}
if (curr.right != null) {
mQueue.offer(curr.right);
}
if (i == 0) {
lastNode = curr.val;
}
}
}
return lastNode;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class MayW2 method getTargetCopy.
/* https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/ */
public final TreeNode getTargetCopy(final TreeNode o, final TreeNode c, final TreeNode t) {
if (o == null && c == null)
return null;
if (t.val == c.val) {
return c;
}
TreeNode resLeft = getTargetCopy(o.left, c.left, t);
TreeNode resRight = getTargetCopy(o.right, c.right, t);
if (resLeft != null)
return resLeft;
return resRight;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class JanuaryW3 method helper.
public TreeNode helper(StringBuilder builder) {
if (builder.length() == 0) {
return null;
} else if (builder.length() <= 2) {
int root_val = 0;
int n = builder.length();
int i = builder.charAt(0) == '-' ? 1 : 0;
while (i < n && builder.charAt(i) != '(') {
root_val = root_val * 10 + builder.charAt(i) - '0';
i++;
}
root_val = builder.charAt(0) == '-' ? -root_val : root_val;
return new TreeNode(root_val);
}
int root_val = 0;
int n = builder.length();
int i = builder.charAt(0) == '-' ? 1 : 0;
while (i < n && builder.charAt(i) != '(') {
root_val = root_val * 10 + builder.charAt(i) - '0';
i++;
}
root_val = builder.charAt(0) == '-' ? -root_val : root_val;
builder.delete(0, i);
Stack<Character> stack = new Stack<>();
int start = 0;
int end = 0;
if (end < builder.length()) {
stack.push(builder.charAt(end++));
while (!stack.isEmpty() && builder.length() > 0) {
char curr = builder.charAt(end++);
if (curr == '(') {
stack.push(curr);
} else if (curr == ')') {
stack.pop();
}
}
}
StringBuilder left_subs;
if (start == end) {
left_subs = new StringBuilder();
} else {
left_subs = new StringBuilder(builder.substring(start + 1, end - 1));
}
start = end;
if (end < builder.length()) {
stack.push(builder.charAt(end++));
while (!stack.isEmpty() && builder.length() > 0) {
char curr = builder.charAt(end++);
if (curr == '(') {
stack.push(curr);
} else if (curr == ')') {
stack.pop();
}
}
}
StringBuilder right_sub;
if (start == end) {
right_sub = new StringBuilder();
} else {
right_sub = new StringBuilder(builder.substring(start + 1, end - 1));
}
TreeNode curr = new TreeNode(root_val);
curr.left = helper(left_subs);
curr.right = helper(right_sub);
return curr;
}
Aggregations