通过并返回指针'对于C++中的二进制搜索树插入
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
TreeNode*& node = find(root, val);
node = new TreeNode(val);
return root;
}
TreeNode*& find(TreeNode*& root, int& val) {
if (root == nullptr) return root;
else if (root->val > val) return find(root->left, val);
else return find(root->right, val);
}
};
我正在学习C ++,并在演讲幻灯片上阅读此代码。该代码是关于将新节点插入二进制搜索树中的。这个想法是找到目标位置,然后将新节点插入到该位置。我可以理解“查找”函数返回“指针”类型的原因。我认为这是因为我们需要修改“查找”功能返回后目标位置的地址。但是,当我们将根节点传递到“查找”函数中时,我不知道为什么还需要使用“对指针的引用”类型。如果我更改treenode*&找到(treenode*& root,int& val)到treenode*&查找(treenode* root,int& val),程序将返回原始树而无需目标插入。谁能帮忙这个问题?先感谢您!
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
TreeNode*& node = find(root, val);
node = new TreeNode(val);
return root;
}
TreeNode*& find(TreeNode*& root, int& val) {
if (root == nullptr) return root;
else if (root->val > val) return find(root->left, val);
else return find(root->right, val);
}
};
I am learning C++ and read this code on a lecture slide. The code is about the insertion of a new node into a binary search tree. The idea is to find the target location and then insert a new node to the location. I can understand the reason for the 'find' function returning a 'reference to pointer' type. I think it is because we need to modify the address of the target location after the 'find' function returns. However, I don't know why we need to use the 'reference to pointer' type also when we pass the root node into the 'find' function. If I change TreeNode*& find(TreeNode*& root, int& val) to TreeNode*& find(TreeNode* root, int& val), the program will return the original tree without the target insertion. Can anyone help with this question? Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将
更改为
treenode*&查找(treenode* root,int& val)
然后查看函数的第一行:这将返回对本地变量的引用。在
insertIntObst
中更改它是未定义的行为,不会更改root
actiber actertintobst 。将第一个值插入空树时逐步浏览代码:
insertintObst
函数可以使用与查找的技巧相同的技巧,并修改root到位:甚至更短:
If you change
find
toTreeNode*& find(TreeNode* root, int& val)
then look at the first line of the function:This would return a reference to a local variable. Changing it in
insertIntoBST
is undefined behavior and will not change theroot
variable insideinsertIntoBST
.Go through the code step by step when inserting the first value into an empty tree:
The
insertIntoBST
function could use the same trick as find and modify the root in place:or even shorter: