Leetcode-919 Complete Binary Tree Inserter(完全二叉树插入器)
Posted Asurudo Jyo の 倉 庫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Leetcode-919 Complete Binary Tree Inserter(完全二叉树插入器)的主要内容,如果未能解决你的问题,请参考以下文章