是温度->;左侧和右侧温度->;左=有什么不同? 我在学习C++中的二叉树,在实践中我有很多时间代替了 温度->左=带有的NULL!温度->左侧
然而,在某些情况下,这会给出一个错误,它们的含义是否不同 例如,这里有一个在c中查找二叉树中的表兄弟的代码++是温度->;左侧和右侧温度->;左=有什么不同? 我在学习C++中的二叉树,在实践中我有很多时间代替了 温度->左=带有的NULL!温度->左侧,c++,data-structures,queue,binary-tree,binary-search-tree,C++,Data Structures,Queue,Binary Tree,Binary Search Tree,然而,在某些情况下,这会给出一个错误,它们的含义是否不同 例如,这里有一个在c中查找二叉树中的表兄弟的代码++ vector<int> Solution::solve(TreeNode* A, int B) { queue<pair<TreeNode *,int>> q; q.push({A,0}); vector<int> ans; int reqdepth=-1; int depth; while(!q.em
vector<int> Solution::solve(TreeNode* A, int B) {
queue<pair<TreeNode *,int>> q;
q.push({A,0});
vector<int> ans;
int reqdepth=-1;
int depth;
while(!q.empty()){
TreeNode *temp=q.front().first;
depth=q.front().second;
if(depth==reqdepth) break;
q.pop();
/* LOOK HERE */ if(temp->left!=NULL && temp->left->val==B || temp->right!=NULL && temp->right->val==B){ /*LOOK HERE */
reqdepth=depth+1;
continue;
}
else {
if(temp->left) q.push({temp->left,depth+1});
if(temp->right) q.push({temp->right,depth+1});
}
}
while(!q.empty()){
ans.push_back(q.front().first->val);
q.pop();
}
return ans;
}
vector Solution::solve(树节点*A,int-B){
队列q;
q、 推送({A,0});
向量ans;
内部需求深度=-1;
智力深度;
而(!q.empty()){
TreeNode*temp=q.front()。首先;
深度=q.前().秒;
如果(深度==reqdepth)中断;
q、 pop();
/*看这里*/if(temp->left!=NULL&&temp->left->val==B | | temp->right!=NULL&&temp->right->val==B){/*看这里*/
reqdepth=深度+1;
继续;
}
否则{
if(temp->left)q.push({temp->left,depth+1});
if(temp->right)q.push({temp->right,depth+1});
}
}
而(!q.empty()){
ans.push_back(q.front().first->val);
q、 pop();
}
返回ans;
}
在这段代码中,如果更改了粗体的行,即使它符合要求,也不能给出正确的解决方案。
请让我知道这些符号是否不同,如果不同,那么如何使用?temp->left=如果temp->left不为空,则NULL为真
!!如果temp->left为空,则temp->left为真=如果temp->left不为空,则NULL为真
!!如果temp->left为空,则temp->left为真=NULL应替换为
temp->left
,而不是!温度->左侧
。下面应该可以做到这一点:
if(temp->left && temp->left->val==B || temp->right && temp->right->val==B)
temp->左=NULL
应替换为temp->left
,而不是!温度->左侧
。下面应该可以做到这一点:
if(temp->left && temp->left->val==B || temp->right && temp->right->val==B)
非空指针转换为
true
,空指针转换为false
因此,转换为布尔值的
temp->left
与temp->left!=空
如果你否认,你会发现
!temp->left
与temp->left==NULL
相同
换句话说,事实与你所相信的恰恰相反。非空指针转换为
true
,空指针转换为false
因此,转换为布尔值的
temp->left
与temp->left!=空
如果你否认,你会发现
!temp->left
与temp->left==NULL
相同
换句话说,真相与你所相信的恰恰相反。
temp->left!=NULL
相当于temp->left
(无否定)。是的,这两件事是不同的。。。事实上是相反的<当temp->left
为零时(!temp->left)的计算结果为true
(或nullptr
)<当temp->left
为非零时,如果(temp->left)计算结果为true
。它与显式<代码>相同(如果-TEMP-LUT.0)= /CUL>,尽管使用现代C++,样式(如果是TEMP->左!= NulLPTR),>代码> TEMP->左!NULL相当于temp->left
(无否定)。是的,这两件事是不同的。。。事实上是相反的<当temp->left
为零时(!temp->left)的计算结果为true
(或nullptr
)<当temp->left
为非零时,如果(temp->left)计算结果为true
。这与显式<代码>相同(如果-TEMP-LUT.0)= /CUL>,尽管使用现代C++,样式(如果是TEMP->左!= NulLPTR)是代码< >这是有意义的!现在我明白了!非常感谢。这很有道理!现在我明白了!非常感谢。现在我明白了。谢谢!现在我明白了,谢谢!