use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class A method rightViewBFS.
public void rightViewBFS(TreeNode root) {
if (root == null) {
return;
}
Queue<TreeNode> mQueue = new LinkedList<>();
mQueue.offer(root);
while (!mQueue.isEmpty()) {
int size = mQueue.size();
for (int i = 0; i < size; i++) {
TreeNode curr = mQueue.poll();
if (i == size - 1) {
result.add(curr.val);
}
if (curr.left != null) {
mQueue.offer(curr.left);
}
if (curr.right != null) {
mQueue.offer(curr.right);
}
}
}
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class A method isEvenOddTree.
/*
* 1609. Even Odd Tree
* https://leetcode.com/problems/even-odd-tree/
* */
public boolean isEvenOddTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int level = 0;
while (!q.isEmpty()) {
int size = q.size();
int prev = 0;
if (level % 2 == 0) {
prev = Integer.MIN_VALUE;
} else {
prev = Integer.MAX_VALUE;
}
while (size-- > 0) {
TreeNode curr = q.poll();
int val = curr.val;
if (level % 2 == 0) {
if (val % 2 == 0 || prev >= val) {
return false;
}
prev = val;
} else {
if (val % 2 != 0 || prev <= val) {
return false;
}
prev = val;
}
if (curr.left != null) {
q.offer(curr.left);
}
if (curr.right != null) {
q.offer(curr.right);
}
}
level++;
}
return true;
}
use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.
the class JulyWeek4 method lowestCommonAncestor.
/**
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
* *
*/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// recurseTree(root, p, q);
// return result;
TreeNode lca = recurseTreeAnother(root, p, q);
int d1 = getShortestDistance(lca, p, 0);
int d2 = getShortestDistance(lca, q, 0);
int totalDistance = d1 + d2;
System.out.println("totalDistance: " + totalDistance);
return lca;
}
use of com.pkumar7.TreeNode in project interview by dqnn.
the class _102BinaryTreeLevelOrderTraversal method levelOrder.
/**
* 102. Binary Tree Level Order Traversal
* Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
*
* For example:
* Given binary tree [3,9,20,null,null,15,7],
* 3
* / \
* 9 20
* / \
* 15 7
* [
* [3],
* [9,20],
* [15,7]
* ]
* time : O(n);
* space : O(n);
* @param root
* @return
*/
public static List<List<Integer>> levelOrder(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);
}
res.add(list);
}
return res;
}
use of com.pkumar7.TreeNode in project interview by dqnn.
the class _1038BinarySearchTreeToGreaterSumTree method convertBST_Moris.
public TreeNode convertBST_Moris(TreeNode root) {
TreeNode cur = root;
int sum = 0;
while (cur != null) {
if (cur.right != null) {
// traverse right subtree.
TreeNode leftMost = cur.right;
while (leftMost.left != null && leftMost.left != cur) {
// locate the left-most node of cur's right subtree.
leftMost = leftMost.left;
}
if (leftMost.left == null) {
// never visit the left-most node yet.
// construct a way back to cur.
leftMost.left = cur;
// explore right.
cur = cur.right;
} else {
// visited leftMost already, which implies now on way back.
// cut off the fabricated link.
leftMost.left = null;
// update sum.
sum += cur.val;
// update node value.
cur.val = sum;
// continue on way back.
cur = cur.left;
}
} else {
// no right child: 1) cur is the right-most of unvisited nodes; 2) must traverse left.
// update sum.
sum += cur.val;
// update node value.
cur.val = sum;
// continue on way back.
cur = cur.left;
}
}
return root;
}
Aggregations