use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _98Test method test1.
@Test
public void test1() {
root = new TreeNode(2);
root.left = new TreeNode(1);
root.right = new TreeNode(3);
assertEquals(true, solution1.isValidBST(root));
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _236 method lowestCommonAncestor.
/**
*We need to find TWO nodes in the tree,
* so we'll have to divide and conquer this tree,
* we need to have two nodes to as the intermediate result,
* also, refer to my earlier drawings:http://www.fishercoder.com/2016/06/23/lowest-common-ancestor-of-a-binary-tree/
* I'm really impressed with myself at that time!
*/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) {
return root;
}
return left != null ? left : right;
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _314 method verticalOrder_using_hashmap.
public List<List<Integer>> verticalOrder_using_hashmap(TreeNode root) {
List<List<Integer>> result = new ArrayList();
if (root == null) {
return result;
}
Queue<TreeNode> bfsQ = new LinkedList();
Queue<Integer> indexQ = new LinkedList();
HashMap<Integer, List<Integer>> map = new HashMap();
bfsQ.offer(root);
// we set the root as index 0, left will be negative, right will be positive
indexQ.offer(0);
int min = 0;
int max = 0;
while (!bfsQ.isEmpty()) {
int qSize = bfsQ.size();
for (int i = 0; i < qSize; i++) {
TreeNode curr = bfsQ.poll();
int index = indexQ.poll();
if (map.containsKey(index)) {
map.get(index).add(curr.val);
} else if (!map.containsKey(index)) {
List<Integer> list = new ArrayList();
list.add(curr.val);
map.put(index, list);
}
if (curr.left != null) {
bfsQ.offer(curr.left);
indexQ.offer(index - 1);
min = Math.min(min, index - 1);
}
if (curr.right != null) {
bfsQ.offer(curr.right);
indexQ.offer(index + 1);
max = Math.max(max, index + 1);
}
}
}
for (int i = min; i <= max; i++) {
result.add(map.get(i));
}
return result;
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _513 method findBottomLeftValue.
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
TreeNode leftMost = root;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode curr = queue.poll();
if (i == 0) {
leftMost = curr;
}
if (curr.left != null) {
queue.offer(curr.left);
}
if (curr.right != null) {
queue.offer(curr.right);
}
}
}
return leftMost.val;
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _144 method preorderTraversal_iterative.
public List<Integer> preorderTraversal_iterative(TreeNode root) {
List<Integer> list = new ArrayList();
if (root == null) {
return list;
}
Deque<TreeNode> stack = new ArrayDeque<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode curr = stack.pop();
list.add(curr.val);
/**
*We push right nodes onto the stack first, since they'll be popped out later than
* the left nodes, to meet the preorder: root -> left -> right.
*/
if (curr.right != null) {
stack.push(curr.right);
}
if (curr.left != null) {
stack.push(curr.left);
}
}
return list;
}
Aggregations