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

來源:互聯網
上載者:User

標籤:front   let   完全二叉樹   vector   span   turn   二叉樹   root   color   

 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                 else22                 {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             else38             {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(完全二叉樹插入器)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.