Search in sources :

Example 16 with TreeNode

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;
    }
}
Also used : TreeNode(com.fishercoder.common.classes.TreeNode)

Example 17 with TreeNode

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;
}
Also used : HashMap(java.util.HashMap) TreeNode(com.fishercoder.common.classes.TreeNode)

Example 18 with TreeNode

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));
}
Also used : TreeNode(com.fishercoder.common.classes.TreeNode) Test(org.junit.Test)

Example 19 with TreeNode

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));
}
Also used : TreeNode(com.fishercoder.common.classes.TreeNode) Test(org.junit.Test)

Example 20 with TreeNode

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));
}
Also used : TreeNode(com.fishercoder.common.classes.TreeNode) Test(org.junit.Test)

Aggregations

TreeNode (com.fishercoder.common.classes.TreeNode)39 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)7 LinkedList (java.util.LinkedList)6 HashMap (java.util.HashMap)2 List (java.util.List)2 Before (org.junit.Before)2 ArrayDeque (java.util.ArrayDeque)1 TreeMap (java.util.TreeMap)1 Ignore (org.junit.Ignore)1