use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _623 method addOneRow.
public TreeNode addOneRow(TreeNode root, int v, int d) {
if (d == 1) {
TreeNode newRoot = new TreeNode(v);
newRoot.left = root;
return newRoot;
} else {
dfs(root, v, d);
return root;
}
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _663 method checkEqualTree.
/**
* The idea is that we use a map to store the sum of each node, then in the end,
* we check if any node has a sum that is exactly half of total sum.
*/
public boolean checkEqualTree(TreeNode root) {
Map<TreeNode, Integer> map = new HashMap<>();
int totalSum = sumForEachNode(root, map);
if (totalSum % 2 != 0 || map.size() < 2) {
return false;
}
for (TreeNode key : map.keySet()) {
if (map.get(key) == totalSum / 2) {
return true;
}
}
return false;
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _230Test method test2.
@Test
public void test2() {
root = new TreeNode(2);
root.left = new TreeNode(1);
k = 1;
assertEquals(1, solution1.kthSmallest(root, k));
assertEquals(1, solution2.kthSmallest(root, k));
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _230Test method test1.
@Test
public void test1() {
root = new TreeNode(1);
k = 1;
assertEquals(1, solution1.kthSmallest(root, k));
assertEquals(1, solution2.kthSmallest(root, k));
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _450Test method test1.
@Test
public void test1() {
root = new TreeNode(5);
root.left = new TreeNode(3);
root.left.left = new TreeNode(2);
root.left.right = new TreeNode(4);
root.right = new TreeNode(6);
root.right.right = new TreeNode(7);
expected = new TreeNode(5);
expected.left = new TreeNode(4);
expected.right = new TreeNode(6);
expected.left.left = new TreeNode(2);
expected.right.right = new TreeNode(7);
assertEquals(expected, test.deleteNode(root, 3));
}
Aggregations