Leetcode-919 Complete Binary Tree Inserter(完全二叉树插入器)

 1 vector<TreeNode> ve(16385,0);
 2 class CBTInserter
 3 {
 4     public:
 5         queue<TreeNode*> q;
 6         int veEnd;
 7         
 8         CBTInserter(TreeNode* root)
 9         {
10             TreeNode rubbish(0);
11             veEnd = 0;
12             ve[veEnd++] = (rubbish);
13             q.push(root);
14             Init();
15             for(int i = 2; i < veEnd; i ++)
16             {
17                 if((i&0x1)==1)
18                 {
19                     ve[i/2].right = &ve[i];
20                 }
21                 else
22                 {
23                     ve[i/2].left = &ve[i];
24                 }
25             }
26         //    cout << "??" << ve.size() << endl;
27         }
28 
29         int insert(int v)
30         {
31             TreeNode t(v);
32             ve[veEnd++] = (t);
33             if((veEnd%2)==1)
34             {
35                 ve[(veEnd-1)/2].left = &ve[veEnd-1];
36             }
37             else
38             {
39                 ve[(veEnd-1)/2].right = &ve[veEnd-1];
40             }
41             return ve[(veEnd-1)/2].val;
42         }
43 
44         TreeNode* get_root()
45         {
46             return &ve[1];
47         }
48 
49         void Init()
50         {
51             while(!q.empty())
52             {
53                 TreeNode t = *(q.front());
54                 ve[veEnd++] = (*(q.front()));
55                 if(q.front()->left)
56                     q.push(q.front()->left);
57                 if(q.front()->right)
58                     q.push(q.front()->right);
59                 q.pop();
60             }
61         }
62 };

哔了狗,一直忘记vector开辟空间以后会把所有元素的地址进行变动,然后疯狂debug

    原文作者:Asurudo
    原文地址: https://www.cnblogs.com/Asurudo/p/9763524.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞