Q: Given the roots of two binary trees p and q, write a function to check if they are the same or not.
solution:
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
// p and q are both null
if (p == null && q == null) return true;
// one of p and q is null
if (q == null || p == null) return false;
if (p.val != q.val) return false;
return isSameTree(p.right, q.right) &&
isSameTree(p.left, q.left);
}
}
I am confused about the "return isSameTree(p.right, q.right) &&
isSameTree(p.left, q.left);" what is the meaning of it, and why there is no condition like "else" before it ??? what it return finally ?
Related
Can Someone explain what is wrong in this code ?
It is failing on testcase s = "aa" and p = "*".
I have followed recursion and dynamic programming code here
Leetcode 44 Wildcard Matching
class Solution {
public boolean isMatch(String s, String p) {
int n = s.length();
int m = p.length();
int[][] dp = new int[n][m];
for(int[] it : dp)
Arrays.fill(it, -1);
return solve(n-1 , m-1, s ,p , dp);
}
public boolean solve(int i, int j, String s, String p, int[][] dp){
if(i < 0 && j < 0) return true;
if(i < 0 && j >=0){
while(j>=0){
if(p.charAt(j) + "" == "*") j--;
else return false;
}
return true;
}
if(j < 0 && i >=0) return false;
if(dp[i][j] != -1){
if(dp[i][j]==1) return true;
return false;
}
if(s.charAt(i) == p.charAt(j) || p.charAt(j) + "" == "?"){
boolean temp = solve(i-1,j-1,s,p,dp);
if(temp == false) dp[i][j] = 0;
else
dp[i][j] = 1;
return temp;
}
if(p.charAt(j) + "" == "*"){
boolean temp = solve(i-1,j,s,p,dp) || solve(i,j-1,s,p,dp);
if(temp == false)
dp[i][j] = 0;
else
dp[i][j] = 1;
return temp;
}
dp[i][j] = 0;
return false;
}
}
I'm trying to compare list of music with releaseDate. But I can retrieve music without releaseDate and when I want to sort them, I got an error.
How can I sort / compare nullable datetime and put null releaseDate to the end?
_followedMusic.sort((a, b) {
if (a.releaseDate != null && b.releaseDate != null)
return a.releaseDate.compareTo(b.releaseDate);
else
// return ??
});
Thank you
If you take a look at the documentation for compareTo:
Returns a value like a Comparator when comparing this to other. That is, it returns a negative integer if this is ordered before other, a positive integer if this is ordered after other, and zero if this and other are ordered together.
https://api.dart.dev/stable/2.10.0/dart-core/Comparable/compareTo.html
So your compareTo should just result in returning the values -1, 0 or 1 according to if the compared object should be before, the same position or after the current object.
So in your case if you want your null entries to be at the start of the sorted list, you can do something like this:
void main() {
final list = ['b', null, 'c', 'a', null];
list.sort((s1, s2) {
if (s1 == null && s2 == null) {
return 0;
} else if (s1 == null) {
return -1;
} else if (s2 == null) {
return 1;
} else {
return s1.compareTo(s2);
}
});
print(list); // [null, null, a, b, c]
}
Or if you want the null at the end:
void main() {
final list = ['b', null, 'c', 'a', null];
list.sort((s1, s2) {
if (s1 == null && s2 == null) {
return 0;
} else if (s1 == null) {
return 1;
} else if (s2 == null) {
return -1;
} else {
return s1.compareTo(s2);
}
});
print(list); // [a, b, c, null, null]
}
Or, as #lrn suggests, make the last example in a more short and efficient way (but maybe not as readable :) ):
void main() {
final list = ['b', null, 'c', 'a', null];
list.sort((s1, s2) => s1 == null
? s2 == null
? 0
: 1
: s2 == null
? -1
: s1.compareTo(s2));
print(list); // [a, b, c, null, null]
}
what about _followdMusic.map((date) => return date ?? 1900.01.01).toList().sort(...)
the date is pseudo code, not sure how to write it. This way you put all unknown dates at one of the ends of the list.
The answer of #julemand101 also can be used with the extension function.
extension DateEx on DateTime? {
int compareToWithNull(DateTime? date2) {
if (this == null && date2 == null) {
return 0;
} else if (this == null) {
return -1;
} else if (date2 == null) {
return 1;
} else {
return this!.compareTo(date2);
}
}
}
This is a recursive check if this is a prime number -- is it correct?
public static boolean isPrimeRecursive (int n,int i){//i eqoual to n
if (n <= 1) {
return false;
}if (i==1){
return false;
}if(n%i==0){
return false;
}
return isPrimeRecursive(n,i--);
}
I wouldn't burden your user with that mysterious second argument but rather present a different method of just one argument, that first deals with numbers less than 2 and even numbers, and then calls into your recursive method with the proper arguments:
private static boolean isPrimeRecursive(int n, int i) {
if (i * i > n) {
return true;
}
if (n % i == 0) {
return false;
}
return isPrimeRecursive(n, i + 2);
}
public static boolean isPrime(int n) {
if (n <= 2 || n % 2 == 0) {
return (n == 2);
}
return isPrimeRecursive(n, 3);
}
public static void main(String[] args) {
System.out.println(isPrime(Integer.parseInt(args[0])));
}
With your code, you should start of i with a value of n-1 since n % n is always true of prime numbers.
Then in your condition (if (i == 1) { ... }, should return true because if the method reaches to 1, then it fulfills all other conditions.
Finally in your return statement return isPrimeRecursive(n, i++);, it is better to use ++i since i++ will increment after the execution of the function with the value of i.
public static boolean isPrimeRecursive (int n,int i){
if (n <= 1) {
return false;
}
if (i == 1) {
return true;
}
if(n % i == 0){
return false;
}
return isPrimeRecursive(n, --i);
}
In your main function, you will then use:
int n = 17;
System.out.println(isPrimeRecursive(n, n-1);
Another way of doing it is to always start i with a value of 2 and increment it's value. From there.
public static boolean isPrimeRecursive (int n, int i) {
if (n <= 2) {
return (n == 2) ? true : false;
}
if (i >= n) {
return true;
}
if (n % i == 0) {
return false;
}
return isPrimeRecursive(n, ++i);
}
Then you simple do:
int n = 17;
System.out.println(isPrimeRecursive(n, 2);
what is the base case here ?? This is fully functional but how it's working?
int isPali(char *s, int l, int r)
{
return ((l == r) || (s[l] == s[r] && isPali(s, l+1, r-1)));
}
int main()
{
char str[100];
scanf("%s", str);
if(isPali(str, 0, strlen(str)-1))
printf("Palindrome\n");
else
printf("Not palindrome\n");
}
It's in the only line of the function. Remember short-circuit logic in evaluating expressions. The or can be changed to
if (l == r)
return True;
else
return (s[l] == s[r] && isPali(s, l+1, r-1));
Of course, you can now apply the short-circuit to that else part:
else
if s[l] == s[r]
return isPali(s, l+1, r-1);
else
return False;
I am going to compare Binary Tree in Golang.But my answer is wrong. Need the third eye to help.
Thanks.
package main
import(
"fmt"
)
type TreeNode struct {
val int
left *TreeNode
right *TreeNode
}
func isSameTree(p *TreeNode , q *TreeNode ) (bool){
if p == nil && q == nil {
return true
}
if p != nil && q == nil{
return false;
}
if p ==nil && q != nil {
return false;
}
if (p.val == q.val) && (isSameTree(p.left,q.left)) && (isSameTree(p.right ,q.left)){
return true;
} else {
return false;
}
}
func main(){
p := &TreeNode{val: 1}
p.left = &TreeNode{val: 2}
p.right = &TreeNode{val: 3}
q := &TreeNode{val: 1}
q.left = &TreeNode{val: 2}
q.right = &TreeNode{val: 3}
isSame := isSameTree(p,q)
fmt.Println("is same?: ", isSame)
}
Go playground link for this code:
https://play.golang.org/p/mTX3aBxh6_
This line has a small mistake;
if (p.val == q.val) && (isSameTree(p.left,q.left)) && (isSameTree(p.right ,q.left)){
It should be;
if (p.val == q.val) && (isSameTree(p.left,q.left)) && (isSameTree(p.right ,q.right)){
If you don't see the difference in the second call to isSameTree you're passing q.left when it is supposed to be q.right.
Updated go play; https://play.golang.org/p/ul9ijG9HLc