zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    LDD3 第7章 Time,Delays and Deferred Work
    4412 gpio读取pwm
    LDD快速参考
    4412 4路pwm输出
    PCB六层板学习(一)
    STM32 TIM3 PWM输出 4路
    4412 学习目录总结
    4412 Linux定时器
    4412 SPI驱动
    4412 i2c驱动
  • 原文地址:https://www.cnblogs.com/Asurudo/p/9763524.html
Copyright © 2011-2022 走看看