(sketch) .equals != ==
Time: 20:31
public static <T> int treeValueCountDFSRecursive(Node<T> root, T target) {
int count = 0;
// base case
if (root == null) {
return 0;
}
// base case
if (root.val == target) {
count = 1;
}
count += treeValueCountDFSRecursive(root.left, target);
count += treeValueCountDFSRecursive(root.right, target);
return count;
}
Time: 20:55
public static <T> int treeValueCountDFSRecursive(Node<T> root, T target) {
int count = 0;
// base case
if (root == null) {
return 0;
}
// base case
if (root.val.equals(target)) {
count = 1;
}
count += treeValueCountDFSRecursive(root.left, target);
count += treeValueCountDFSRecursive(root.right, target);
return count;
}
Posts with tag "sketch"