Runtime error - using structs in cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    TreeNode* bstFromPreorder(vector<int>& preorder) {
        int size = preorder.size();
        TreeNode root = TreeNode(preorder[0]);
        stack<TreeNode*> st;
        int v;
        TreeNode newNode = TreeNode(-1);
        TreeNode* prevNode;
        st.push(&root);
        for(int i=1;i<size;i++) {
            v = preorder[i];
            prevNode = st.top(); 
            newNode = TreeNode(v);
            while(!st.empty() && prevNode->val<v) {
              st.pop();
              prevNode = st.top();
            }
            
            if (st.empty()) {
                root.right = &newNode;
            }

            if (v<prevNode->val){
                prevNode->left = &newNode;
            } else {
                 prevNode->right = &newNode;
            }

            st.push(&newNode);
            
        }

        return &root;       
        
    }
};



Runtime Error:
Line 20: Char 37: runtime error: member access within misaligned address 0x000000000002 for type 'TreeNode', which requires 8 byte alignment (__TreeNodeUtils__.cpp)
0x000000000002: note: pointer points here
<memory cannot be printed>
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior __TreeNodeUtils__.cpp:30:37
Last edited on
Try this code. It works:
https://onlinegdb.com/S10NfFsu8

You appear to be not only returning the address of a local object but also pushing the address of another local object into the tree.

And again, you are shit at asking questions! :-)

And sticking your code in a class called Solution is abnormal in C++. That's more of a Java thing, which I assume is where you picked that up.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    TreeNode* bstFromPreorder(vector<int>& preorder) {
          
        
    } 
}; 
I hv been provided with this template so I cant help @dutch.
Last edited on
Well that explains that then. :-)
If you still want some help maybe you could give me a clue about what the program is supposed to do.
I see what you're doing now. I've never heard of building a bst from the preorder traversal data. It's interesting, and I think maybe a good way of saving tree data so that the tree can be efficiently recreated with its original structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <stack>

struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(), right() {}
};

class Solution {
public:
    static TreeNode* bstFromPreorder(std::vector<int>& v)
    {
        if (v.empty()) return nullptr;
        TreeNode* root = new TreeNode(v[0]);
        std::stack<TreeNode*> st;
        st.push(root);
        for (unsigned i = 1; i < v.size(); ++i)
        {
            TreeNode* p = nullptr;
            while (!st.empty() && v[i] > st.top()->val)
            {
                p = st.top();
                st.pop();
            }
            st.push((p ? p->right : st.top()->left) = new TreeNode(v[i]));
        }
        return root;
    }

    static void print(TreeNode* node)
    {
        if (!node) return;
        print(node->left);
        std::cout << node->val << ' ';
        print(node->right);
    }
};

int main()
{
    // Preorder traversal
    std::vector<int> v{ 20, 16, 5, 18, 17, 19, 60, 85, 70 };

    auto t = Solution::bstFromPreorder(v);

    Solution::print(t);
    std::cout << '\n';
}

Cool. I think i have some bug in logic of my code causing segmentation fault
Topic archived. No new replies allowed.