就我的逻辑而言,我使用两个不同的数组来存储所有叶子,然后比较这些数组以查看叶子是否确实相同,但我的测试用例失败了(例如[3,5,1, 6,2,9,8,空,空,7,4] [3,5,1,6,7,4,2,空,空,空,空,空,空,9,8])。提前致谢!
''' 类解决方案 {
static int array1[] = new int[50];
static int array2[] = new int[50];
static int q = 0;
static int r = 0;
public boolean compareLeaves(int arr1[], int arr2[])
{
for(int i = 0; i <array1.length ;i ++)
{
if(array1[i] != array2[i])
{
return false;
}
}
return true;
}
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
if(root1 == null || root2 == null)
{
return false;
}
if(root1.left == null && root1.right == null)
{
array1[q] =root1.val ;
q++;
}
if(root2.left == null && root2.right == null)
{
array2[r] =root2.val ;
r++;
}
leafSimilar(root1.left,root2.left);
leafSimilar(root1.right,root2.right);
return compareLeaves(array1,array2);
}
} '''
撒科打诨
牧羊人nacy
精慕HU
相关分类