I'm working on a leetcode problem: Given the root of a binary tree, return the inorder traversal of its nodes' values. I was thinking I can push the right, root and left value on a stack and then pop them to put them to a vector to revert the order. However, I am getting this error:() line 22 is the vector.push_back(root->right->val). I'm not sure why I am getting that
Line 22: Char 43: runtime error: member access within null pointer of type 'TreeNode' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:31:43
/**
* 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:
vector<int> inorderTraversal(TreeNode* root) {
std::stack<TreeNode*> stack;
std::vector<int> vector;
stack.push(root->left);
while(!stack.empty()){
root->right = stack.top();
stack.pop();
vector.push_back(root->right->val);
if(root){
stack.push(root);
}
if(root->left){
stack.push(root->left);
}
}
return vector;
}
};
Related
/**
* 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!
If you change find to TreeNode*& find(TreeNode* root, int& val) then look at the first line of the function:
if (root == nullptr) return root;
This would return a reference to a local variable. Changing it in insertIntoBST is undefined behavior and will not change the root variable inside insertIntoBST.
Go through the code step by step when inserting the first value into an empty tree:
NodeTree *tree = nullptr;
tree = insertIntoBST(tree, 42);
The insertIntoBST function could use the same trick as find and modify the root in place:
void insertIntoBST(TreeNode*& root, int val) {
TreeNode*& node = find(root, val);
node = new TreeNode(val);
}
NodeTree *tree = nullptr;
insertIntoBST(tree, 42);
insertIntoBST(tree, 23);
insertIntoBST(tree, 666);
or even shorter:
void insertIntoBST(TreeNode*& root, int val) {
find(root, val) = new TreeNode(val);
}
I was going through a problem to delete an element from the BST, and I wrote the following program:
/**
* 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* deleteNode(TreeNode* r, int k) {
if(r==0)
return 0;
if(r->val<k)
{
r->right=deleteNode(r->right,k);
return r;
}
else if(r->val>k)
{
r->left=deleteNode(r->left,k);
return r;
}
if(r->left==0)
return r->right;
if(r->right==0)
return r->left;
TreeNode *p=r->right;
while(p->left)
p=p->left;
p->left=r->left;
return r->right;
}
};
The main problem that I want to solve is to include the last while loop as the part of recursion.
Note that I do not want to use any other function, like isMin(), etc.
I was doing a Leetcode challenge and my code used about .5 kb of memory more than other submissions that looked more complex. The challenge itself was inverting a binary tree. Although I submitted my solution multiple times to take the average values of runtime and memory usage for my code.
My code below runs on average for 3ms and takes 8.5 kb of memory after 10 trial submissions.
/**
* 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* invertTree(TreeNode* root) {
if(root == nullptr) return root;
invertTree(root -> left);
invertTree(root -> right);
TreeNode* tmp = root ->left;
root -> left = root -> right;
root -> right = tmp;
return root;
}
};
The code below is the sample submission that Leetcode shows as an example of 0ms runtime and 8kb of memory usage
class Solution {
public:
void invert(TreeNode* root)
{
if(root==nullptr)
return;
invert(root->left);
invert(root->right);
TreeNode* temp = root->right;
root->right = root->left;
root->left = temp;
}
TreeNode* invertTree(TreeNode* root) {
if(root==nullptr)
return nullptr;
invert(root);
return root;
}
};
I am wondering how the code given uses two functions and uses less memory than my solution? Additionally, I am relatively new to programming, and am wondering if this case is exemplary of deeper mechanic within the C++ language or too nitpick-y to warrant any concern. Thanks for reading
I keep getting the initialized value of max as my output for some reason
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
void maxi(struct TreeNode* A,int t,int k){
if(A->left==NULL&&A->right==NULL&&t>k) k=t;
if(A->left!=NULL) maxi(A->left,t+1,k);
if(A->right!=NULL) maxi(A->right,t+1,k);
}
int Solution::maxDepth(TreeNode* A) {
int l=0;
maxi(A,1,l);
return l;
}
The function keeps outputting initialized value of l. In this case 0.
Working in C++ here. I am trying to develop a specific program that creates a BST from its preorder traversal. Here's the first batch of code:
class Node {
private:
int val;
Node *left;
Node *right;
public:
Node () : val(0), left(NULL), right(NULL) {}
Node (int v) : val(v), left(NULL), right(NULL) {}
Node (int v, Node *l, Node *r) : val(v), left(l), right(r) {}
Node& operator = (Node& rhs) {
if (&rhs == this) return *this;
val = rhs.val;
left = rhs.left;
right = rhs.right;
return *this;
}
}*root;
I've taken out some of the functions but that's the basics. Now here's the other piece of code:
Node *temp = new Node();
if (preorder[trav] <= N)
{
temp = root;
root->goLeft(pre_ordered, traverse);
traverse++;
}
else
{
temp = root;
root->goRight(pre_order, traverse);
traverse++;
}
However, I then get " error: invalid conversion from 'Node*' to 'int' " on the line "temp = root". Any ideas? It seems to me I'm setting one node equal to the other so I don't understand what's wrong. Looked online and found nothing helpful. Some similar errors, but just different enough to not be useful.
I'll be upfront that this is for a school assignment so I'm not looking for direct answers. Just hints about what I need to change or where I need to look would be great! Thanks.
EDIT: Exact error is:
main.cpp:160:15: error: invalid conversion from ‘Node*’ to ‘int’ [-fpermissive]
temp = root;
^
EDIT I found it. I apologize.
I was stupid and didn't look hard enough to find that I was redefining "temp" as an int in the main function. Sorry for wasting your time guys.
Can't answer my own question since not enough rep.
The names of the members you have declared do not match what you used in the code. For example, you declared int value but reference it by the name val
Corrected code that compiles is:
class Node {
private:
int val;
Node *left;
Node *right;
public:
Node () : val(0), left(NULL), right(NULL) {}
Node (int v) : val(v), left(NULL), right(NULL) {}
Node (int v, Node *l, Node *r) : val(v), left(l), right(r) {}
Node& operator = (Node& rhs) {
if (&rhs == this) return *this;
val = rhs.val;
left = rhs.left;
right = rhs.right;
return *this;
}
}*root;
int main()
{
Node *temp = new Node();
temp = root;
return 0;
}
I know you didn't want a direct answer, but it seemed like an actual mistake, and not a lack of understanding.