use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _98Test method test2.
@Test
public void test2() {
root = new TreeNode(0);
assertEquals(true, solution1.isValidBST(root));
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _314 method verticalOrder_using_treemap.
public List<List<Integer>> verticalOrder_using_treemap(TreeNode root) {
List<List<Integer>> result = new ArrayList();
if (root == null) {
return result;
}
Queue<TreeNode> bfsQ = new LinkedList();
Queue<Integer> indexQ = new LinkedList();
TreeMap<Integer, List<Integer>> map = new TreeMap();
bfsQ.offer(root);
// we set the root as index 0, left will be negative, right will be positive
indexQ.offer(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);
}
if (curr.right != null) {
bfsQ.offer(curr.right);
indexQ.offer(index + 1);
}
}
}
for (int i : map.keySet()) {
result.add(map.get(i));
}
return result;
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class TreeUtils method main.
public static void main(String... args) {
// test random int generator
List<Integer> treeValues = CommonUtils.randomIntArrayGenerator(24);
List<Integer> treeValues2 = Arrays.asList(0, 1, 2, 3, 4, 5, 6);
// test tree construction
// TreeNode root1 = bruteForceConstructBinaryTree(treeValues2);
// inOrderTraversal(root1);
// printBinaryTree(root1);
// test tree construction
TreeNode root2 = constructBinaryTree(treeValues);
inOrderTraversal(root2);
printBinaryTree(root2);
List<Integer> treeVals = new ArrayList<>(Arrays.asList(1, null, 2, 3));
CommonUtils.printList(treeVals);
root2 = constructBinaryTree(treeVals);
// inOrderTraversal(root2);
printBinaryTree(root2);
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class TreeUtils method printNodeInternal.
private static void printNodeInternal(List<TreeNode> list, int level, int maxLevel) {
if (list.isEmpty() || CommonUtils.isAllElementsNull(list)) {
return;
}
int floor = maxLevel - level;
int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
int firstSpaces = (int) Math.pow(2, (floor)) - 1;
int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;
CommonUtils.printWhitespaces(firstSpaces);
List<TreeNode> newNodes = new ArrayList<>();
for (TreeNode node : list) {
if (node != null) {
System.out.print(node.val);
newNodes.add(node.left);
newNodes.add(node.right);
} else {
newNodes.add(null);
newNodes.add(null);
System.out.print(" ");
}
CommonUtils.printWhitespaces(betweenSpaces);
}
System.out.println("");
for (int i = 1; i <= endgeLines; i++) {
for (int j = 0; j < list.size(); j++) {
CommonUtils.printWhitespaces(firstSpaces - i);
if (list.get(j) == null) {
CommonUtils.printWhitespaces(endgeLines + endgeLines + i + 1);
continue;
}
if (list.get(j).left != null) {
System.out.print("/");
} else {
CommonUtils.printWhitespaces(1);
}
CommonUtils.printWhitespaces(i + i - 1);
if (list.get(j).right != null) {
System.out.print("\\");
} else {
CommonUtils.printWhitespaces(1);
}
CommonUtils.printWhitespaces(endgeLines + endgeLines - i);
}
System.out.println("");
}
printNodeInternal(newNodes, level + 1, maxLevel);
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class TreeUtils method constructBinaryTree.
/**
* This method is to construct a normal binary tree. The input reads like
* this for [5, 3, 6, 2, 4, null, null, 1]:
* 5
* / \
* 3 6
* / \ / \
* 2 4 # #
* /
* 1
*/
@Notes(context = "This is usually how Leetcode OJ passes a binary tree into testing: " + "https://leetcode.com/faq/#binary-tree, I wrote this function for my own ease of testing when copying" + "the test case from Leetcode in the form of [1, null, 2, 3].")
public static TreeNode constructBinaryTree(List<Integer> treeValues) {
TreeNode root = new TreeNode(treeValues.get(0));
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
for (int i = 1; i < treeValues.size(); i++) {
TreeNode curr = queue.poll();
if (treeValues.get(i) != null) {
curr.left = new TreeNode(treeValues.get(i));
queue.offer(curr.left);
}
if (++i < treeValues.size() && treeValues.get(i) != null) {
curr.right = new TreeNode(treeValues.get(i));
queue.offer(curr.right);
}
}
return root;
}
Aggregations