什么会导致 return 语句在 if 代码块中不起作用
What could cause a return statement not to work in an if code block
我正在尝试比较两个二叉树以查看它们在结构和值上是否相等,但在算法中的某个时刻,将 return 语句放在 if 代码块中时不起作用对于我想要比较的值。
即
let a = "myVal"
let b = "myVal"
if(a = b){
return false
}
以上内容不仅适用于我要比较的变量,而且适用于所有其他变量。
我检查了两个变量的类型和值,它们确实相等。
此外,当我在 if 代码块中抛出错误时,它会在满足条件时起作用,但 return
语句不只是起作用。
这是完整的代码
function compare(a, b){
if(a === null && b === null){
return true;
}
if(typeof a === 'object' && typeof b === 'object'){
// compare their structures
let aRoot = Object.keys(a);
let bRoot = Object.keys(b);
if(aRoot.length !== bRoot.length){
console.log('0')
return false; //Trees are of different structures
}
//Loop through the roots of the tree
for(let i in aRoot){
if(aRoot[i] !== bRoot[i]){
//Make sure the roots are represented with equal names
console.log('1')
return false;
}
let aValue = a[aRoot[i]];
let bValue = b[bRoot[i]];
if(typeof aValue !== typeof bValue){
console.log('2')
return false
}
if(aValue !== null && bValue !== null){
//If they are both of the same types compare their values check if they are child nodes or not
if(typeof aValue !== 'object'){
//Here's the main problem
if(aValue !== bValue){
// console.log("aValue : ", aValue, " bValue : ", bValue)
// console.log("aValue type : ", typeof aValue, " bValue type : ", typeof bValue)
return false;
}
}
else{
// console.log('a ', aValue)
compare(aValue, bValue);
}
}
}
}
return true;
}
let aNode = {val: 1, left: null, right: null, d: {val: 1, left: null, right: null, f: {val: 2, left: null, right: null}}};
let bNode = {val: 1, left: null, right: null, d: {val: 3/* This differs from aNode*/, left: null, right: null, f: {val: 2, left: null, right: null}}};
console.log(compare(aNode, bNode))
它适用于没有根节点的树,但不适用于有根节点的树。
问题出在代码的 if(aValue !== bValue)
部分。该代码块不仅 return false,而且在抛出错误时,它仍然有效。
你的递归调用...compare(aValue, bValue);
应该是 return compare(aValue, bValue);
否则你会丢失调用的返回值
function compare(a, b){
if(a === null && b === null){
return true;
}
if(typeof a === 'object' && typeof b === 'object'){
// compare their structures
let aRoot = Object.keys(a);
let bRoot = Object.keys(b);
if(aRoot.length !== bRoot.length){
console.log('0')
return false; //Trees are of different structures
}
//Loop through the roots of the tree
for(let i in aRoot){
if(aRoot[i] !== bRoot[i]){
//Make sure the roots are represented with equal names
console.log('1')
return false;
}
let aValue = a[aRoot[i]];
let bValue = b[bRoot[i]];
if(typeof aValue !== typeof bValue){
console.log('2')
return false
}
if(aValue !== null && bValue !== null){
//If they are both of the same types compare their values check if they are child nodes or not
if(typeof aValue !== 'object'){
//Here's the main problem
if(aValue !== bValue){
// console.log("aValue : ", aValue, " bValue : ", bValue)
// console.log("aValue type : ", typeof aValue, " bValue type : ", typeof bValue)
return false;
}
}
else{
// console.log('a ', aValue)
return compare(aValue, bValue);
// Check this ------^----^
}
}
}
}
return true;
}
let aNode = {val: 1, left: null, right: null, d: {val: 1, left: null, right: null, f: {val: 2, left: null, right: null}}};
let bNode = {val: 1, left: null, right: null, d: {val: 3/* This differs from aNode*/, left: null, right: null, f: {val: 2, left: null, right: null}}};
console.log(compare(aNode, bNode))
能否请您试试这段代码,我认为它可以解决您的问题。
/* Given two trees, return true if they are structurally identical */
int identicalTrees(node* a, node* b)
{
/*1. both empty */
if (a == NULL && b == NULL)
return 1;
/* 2. both non-empty -> compare them */
if (a != NULL && b != NULL)
{
return
(
a->data == b->data &&
identicalTrees(a->left, b->left) &&
identicalTrees(a->right, b->right)
);
}
/* 3. one empty, one not -> false */
return 0;
}
然后将其用作:
if(identicalTrees(a, b))
cout << "Both tree are identical.";
else
cout << "Trees are not identical.";
我正在尝试比较两个二叉树以查看它们在结构和值上是否相等,但在算法中的某个时刻,将 return 语句放在 if 代码块中时不起作用对于我想要比较的值。 即
let a = "myVal"
let b = "myVal"
if(a = b){
return false
}
以上内容不仅适用于我要比较的变量,而且适用于所有其他变量。
我检查了两个变量的类型和值,它们确实相等。
此外,当我在 if 代码块中抛出错误时,它会在满足条件时起作用,但 return
语句不只是起作用。
这是完整的代码
function compare(a, b){
if(a === null && b === null){
return true;
}
if(typeof a === 'object' && typeof b === 'object'){
// compare their structures
let aRoot = Object.keys(a);
let bRoot = Object.keys(b);
if(aRoot.length !== bRoot.length){
console.log('0')
return false; //Trees are of different structures
}
//Loop through the roots of the tree
for(let i in aRoot){
if(aRoot[i] !== bRoot[i]){
//Make sure the roots are represented with equal names
console.log('1')
return false;
}
let aValue = a[aRoot[i]];
let bValue = b[bRoot[i]];
if(typeof aValue !== typeof bValue){
console.log('2')
return false
}
if(aValue !== null && bValue !== null){
//If they are both of the same types compare their values check if they are child nodes or not
if(typeof aValue !== 'object'){
//Here's the main problem
if(aValue !== bValue){
// console.log("aValue : ", aValue, " bValue : ", bValue)
// console.log("aValue type : ", typeof aValue, " bValue type : ", typeof bValue)
return false;
}
}
else{
// console.log('a ', aValue)
compare(aValue, bValue);
}
}
}
}
return true;
}
let aNode = {val: 1, left: null, right: null, d: {val: 1, left: null, right: null, f: {val: 2, left: null, right: null}}};
let bNode = {val: 1, left: null, right: null, d: {val: 3/* This differs from aNode*/, left: null, right: null, f: {val: 2, left: null, right: null}}};
console.log(compare(aNode, bNode))
它适用于没有根节点的树,但不适用于有根节点的树。
问题出在代码的 if(aValue !== bValue)
部分。该代码块不仅 return false,而且在抛出错误时,它仍然有效。
你的递归调用...compare(aValue, bValue);
应该是 return compare(aValue, bValue);
否则你会丢失调用的返回值
function compare(a, b){
if(a === null && b === null){
return true;
}
if(typeof a === 'object' && typeof b === 'object'){
// compare their structures
let aRoot = Object.keys(a);
let bRoot = Object.keys(b);
if(aRoot.length !== bRoot.length){
console.log('0')
return false; //Trees are of different structures
}
//Loop through the roots of the tree
for(let i in aRoot){
if(aRoot[i] !== bRoot[i]){
//Make sure the roots are represented with equal names
console.log('1')
return false;
}
let aValue = a[aRoot[i]];
let bValue = b[bRoot[i]];
if(typeof aValue !== typeof bValue){
console.log('2')
return false
}
if(aValue !== null && bValue !== null){
//If they are both of the same types compare their values check if they are child nodes or not
if(typeof aValue !== 'object'){
//Here's the main problem
if(aValue !== bValue){
// console.log("aValue : ", aValue, " bValue : ", bValue)
// console.log("aValue type : ", typeof aValue, " bValue type : ", typeof bValue)
return false;
}
}
else{
// console.log('a ', aValue)
return compare(aValue, bValue);
// Check this ------^----^
}
}
}
}
return true;
}
let aNode = {val: 1, left: null, right: null, d: {val: 1, left: null, right: null, f: {val: 2, left: null, right: null}}};
let bNode = {val: 1, left: null, right: null, d: {val: 3/* This differs from aNode*/, left: null, right: null, f: {val: 2, left: null, right: null}}};
console.log(compare(aNode, bNode))
能否请您试试这段代码,我认为它可以解决您的问题。
/* Given two trees, return true if they are structurally identical */
int identicalTrees(node* a, node* b)
{
/*1. both empty */
if (a == NULL && b == NULL)
return 1;
/* 2. both non-empty -> compare them */
if (a != NULL && b != NULL)
{
return
(
a->data == b->data &&
identicalTrees(a->left, b->left) &&
identicalTrees(a->right, b->right)
);
}
/* 3. one empty, one not -> false */
return 0;
}
然后将其用作:
if(identicalTrees(a, b))
cout << "Both tree are identical.";
else
cout << "Trees are not identical.";