BST的遍历
in Solutions with 131 comments

BST的遍历

in Solutions with 131 comments

BST的遍历

InOrder

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) 
    {
        vector<int> result;
        if(root==NULL)return result;
        stack<TreeNode*> aStack;
        TreeNode* cur=root;
        
        while(cur!=NULL||!aStack.empty())
        {
            while(cur!=NULL)
            {
                aStack.push(cur);
                cur=cur->left;
            }
            cur=aStack.top();
            aStack.pop();
            result.push_back(cur->val);
            cur=cur->right;
        }
        return result;
    }
};

PreOrder

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) 
    {
        vector<int> result;
        if(root==NULL)return result;
        stack<TreeNode*> aStack;
        TreeNode* cur=root;
        
        while(cur!=NULL||!aStack.empty())
        {
            while(cur!=NULL)
            {
                result.push_back(cur->val);
                aStack.push(cur);
                cur=cur->left;
            }
            cur=aStack.top();
            aStack.pop();
            cur=cur->right;
        }
        return result;
    }
};

PostOrder

后序遍历其实相当于先访问根节点,再访问右子树,最后访问左子树。然后把得到的序列整个reverse即可得到结果。这边用了deque,避免了reverse,插入开销为O(1)

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) 
    {
        deque<int> result;
        if(root==NULL)return {};
        stack<TreeNode*> aStack;
        TreeNode* cur=root;
        while(cur||!aStack.empty())
        {
            while(cur)
            {
                result.push_front(cur->val);
                aStack.push(cur);
                cur=cur->right;
            }
            cur=aStack.top;
            aStack.pop();
            cur=cur->left;
        }
        vector<int> realResult(result.begin(),result.end());
        return realResult;
    }
};
Responses
  1. Cialis Info Articulo 23 Forum Viagra Alcool cialis 20mg for sale Cialis Ojos Rojos Priligy Online Propecia Customer Service

    Reply
    1. @StevRing

      [url=http://metformin0l1.com]cheap metformin[/url]
      [url=http://sildenafild9z.com]foods that contain sildenafil [/url]

      Reply
    2. @StevRing

      [url=http://allopurinol0l1.com]www.allopurinol0l1.com[/url]
      [url=http://propecia0l1.com]propecia 2 mg [/url]

      Reply
    3. @StevRing

      [url=http://sildenafild9z.com]http://www.sildenafild9z.com[/url]
      [url=http://cialis0z.com]reviews on generic cialis [/url]
      [url=http://cialis0z.com]www.cialis0z.com[/url]
      [url=http://zithromax0l1.com]azithromycin 1000 mg price [/url]

      Reply
    4. @StevRing

      [url=http://zithromax0l1.com]http://zithromax0l1.com[/url]
      [url=http://cialisa1c.com]cialis super active, 40 mg [/url]
      [url=http://lisinopril0l1.com]generic lisinopril[/url]
      [url=http://prednisone0l1.com]blue clues dailymotion prednisone for dogs [/url]
      [url=http://tadalafila1c.com]http://www.tadalafila1c.com[/url]
      [url=http://viagrad9z.com]generic blood pressure medicine names [/url]

      Reply
    5. @StevRing

      [url=http://prednisone0l1.com]prednisone canine bladder leak [/url]

      Reply
    6. @StevRing

      [url=http://vardenafil0i0.com]levitra generic vardenafil cheap [/url]

      Reply
    7. @StevRing

      does prednisone affect the eyes

      Reply
      1. @Charlesgex

        generic cialis
        healthcare provider cialis
        generic cialis.

        Reply
    8. @StevRing

      generic xenical

      Reply
    9. @StevRing

      www.1stbaclofen.com

      Reply
    10. @StevRing

      will prednisone help a cough

      Reply
    11. @StevRing

      buy sildenafil
      300 mg allopurinol vs 40mg uloric

      Reply
    12. @StevRing

      allopurinol
      prednisone 10 mg picture

      Reply
    13. @StevRing

      http://allopurinol0l1.com
      sildenafil online shop
      http://xenical0l1.com
      5 mg cialis best price
      buy prednisolone
      price of 4 tablets pack of viagra of 100 mga pack of

      Reply
    14. @StevRing

      allopurinol
      ndc code for 75 mg tadalafil
      tadalafil
      prednisolone-gatifloxacin-bromfenac what used for

      Reply
    15. @StevRing

      http://www.sildenafila1c.com

      Reply
    16. @StevRing

      sildenafil 100mg "no prescription" cheap india

      Reply
    17. @StevRing

      tadalafil, sildenafil

      Reply
    18. @StevRing

      vardenafil cyp450

      Reply
    19. @StevRing

      generic allopurinol
      sildenafil 50 mg efectos

      Reply
    20. @StevRing

      adcirca tadalafil 20 mg

      Reply
    21. @StevRing

      http://xenical0l1.com

      Reply
    22. @StevRing

      generic zithromax
      generic cialis online for sale
      www.sildenafild9z.com
      zithromax brand and generic name
      www.lisinopril0l1.com
      gummy tadalafil

      Reply
    23. @StevRing

      http://xenical0l1.com
      where to buy tadalafil in singapore

      Reply
    24. @StevRing

      http://www.viagrav3u.com
      cialis ed 5 mg

      Reply
    25. @StevRing

      generic tadalafil
      generic viagra images
      http://www.tadalafila1c.com
      metformin 500 mg can thia deug cause livwr problwms?
      http://sildenafild9z.com
      best ed pills online

      Reply
    26. @StevRing

      generic tadalafil
      is prednisone an inhaled stwroid?
      http://www.propecia0l1.com
      what is the trade name for sildenafil citrate?

      Reply
    27. @StevRing

      http://metformin0l1.com

      Reply
    28. @StevRing

      what is vardenafil

      Reply
    29. @StevRing

      at 80 years of age what is recommended dose of levitra

      Reply
    30. @StevRing

      when will levitra go generic

      Reply
    31. @StevRing

      cialis
      cialis price hyvee

      Reply
    32. @StevRing

      buy xenical boots

      Reply
    33. @StevRing

      allopurinol
      allopurinol without prescription
      cheap lisinopril
      alpha lipoic acid with metformin
      www.prozac0l1.com
      candian pharmacy cialis

      Reply
    34. @StevRing

      www.xenical0l1.com
      dos and don'ts of taking zithromax

      Reply
    35. @StevRing

      how much prozac

      Reply
    36. @StevRing

      generic tadalafil
      wife told husband's viagra
      www.viagrav3u.com
      100 mg sildenafil citrate from canada

      Reply
    37. @StevRing

      where can i buy stendra

      Reply
    38. @StevRing

      buy advair

      Reply
    39. @StevRing

      fda recall for sildenafil containing products

      Reply
    40. @StevRing

      http://www.tadalafild9z.com
      canadian pharmacy cialis

      Reply
    41. @StevRing

      buy prednisone 20mg without prescription

      Reply
    42. @StevRing

      generic viagra
      side effects of prednisolone acetate

      Reply
    43. @StevRing

      cheap propecia
      xenical medication
      cheap zithromax
      natural tadalafil
      http://www.viagrad9z.com
      tuf 20 tadalafil softsules mg sale online

      Reply
    44. @StevRing

      generic cialis
      prednisolone acetate 1 ophthalmic suspension after retina surgery recovery
      http://prozac0l1.com
      drug metformin hcl 500 mg

      Reply
    45. @StevRing

      www.zithromax0l1.com

      Reply
    46. @StevRing

      1 time free levitra coupon

      Reply
    47. @StevRing

      generic baclofen

      Reply
    48. @StevRing

      levitra 10 mg ne iЕџe yarar

      Reply
    49. @StevRing

      www.tadalafila1c.com
      the dangers of metformin

      Reply
    50. @StevRing

      http://prednisone0l1.com
      cost of tadalafil

      Reply
    51. @StevRing

      z-pack zithromax antibiotic for kids

      Reply
    52. @StevRing

      buy cialis
      viagra covered on bluecross fep basic
      generic sildenafil
      anime viagra mistake xx
      generic sildenafil
      allopurinol and ulcerative colitis

      Reply
    53. @StevRing

      cheap cialis

      Reply
    54. @StevRing

      buy metformin
      sildenafil order
      http://www.lisinopril0l1.com
      if you take 10 mg prednisone daily will you need ot wean off

      Reply
    55. @StevRing

      buying cialis safely online

      Reply
    56. @StevRing

      prozac
      buy zithromax online

      Reply
    57. @StevRing

      buy viagra
      primary reason for prozac
      www.allopurinol0l1.com
      cialis tablet
      www.xenical0l1.com
      viagra 30 day free trial coupon

      Reply
    58. @StevRing

      erythromycin

      Reply
    59. @StevRing

      20 mg tadalafil

      Reply
    60. @StevRing

      sildenafil citrate buy online india

      Reply
    61. @StevRing

      generic metformin
      does sildenafil stay active longer if kept in fridge?

      Reply
    62. @StevRing

      http://cialis0z.com
      metformin hcl 500 mg 24hr sa tab

      Reply
    63. @StevRing

      cheap prozac

      Reply
    64. @StevRing

      zithromax
      prednisone get you high
      sildenafil
      sildenafil and lisinopril
      www.lisinopril0l1.com
      buy sildenafil online

      Reply
    65. @StevRing

      viagra erection duration

      Reply
    66. @StevRing

      http://metformin0l1.com
      prednisone 20 mg para que sirve
      http://metformin0l1.com
      finasteride propecia buy

      Reply
    67. @StevRing

      glucocorticoid activity medrol vs prednisone

      Reply
    68. @StevRing

      tuf 20 tadalafil vs cialis

      Reply
    69. @StevRing

      sildenafil
      ic sildenafil vs viagra

      Reply
    70. @StevRing

      cheap prednisone
      cialis commercial with man and woman in separate bath tubs

      Reply
    71. @StevRing

      is allopurinol a nsaid

      Reply
    72. @StevRing

      sildenafil
      how long before surgery should you stop taking sildenafil
      zithromax
      chewable tadalafil pills
      http://lisinopril0l1.com
      what is the life shelflife for xenical

      Reply
    73. @StevRing

      primary reason for prozac

      Reply
    74. @StevRing

      cheap lisinopril
      where to buy xenical in ghana
      buy tadalafil
      ocd prescription

      Reply
    75. @StevRing

      cheap mobic

      Reply
    76. @StevRing

      iric for ventolin

      Reply
    77. @StevRing

      canadian cialis no prescription

      Reply
    78. @StevRing

      http://www.sildenafila1c.com
      fildena 100 mg reviews

      Reply
    79. @StevRing

      prednisone
      what is fluoxetine 20 mg

      Reply
    80. @StevRing

      xenical
      cialis otc walmart
      http://www.propecia0l1.com
      xenical facts
      http://www.viagrad9z.com
      what adverse affects can bupropion and prozac have when taken together

      Reply
    81. @StevRing

      www.propecia0l1.com

      Reply
    82. @StevRing

      www.viagrav3u.com

      Reply
    83. @StevRing

      cheap zithromax
      zithromax works after 5 days
      www.tadalafild9z.com
      tadalafil 20 mg side effects

      Reply
    84. @StevRing

      cialis best price canada

      Reply
    85. @StevRing

      http://propecia0l1.com

      Reply
    86. @StevRing

      buy zithromax
      cialis vs cataflam

      Reply
    87. @StevRing

      can acyclovir be crushed

      Reply
    88. @StevRing

      tadalafil 5mg generic

      Reply
    89. @StevRing

      www.propecia0l1.com
      long lasting sex pills pharmacy

      Reply
    90. @StevRing

      buy metformin
      non prescription erectile dysfunction drugs

      Reply
    91. @StevRing

      http://tadalafild9z.com

      Reply
    92. @StevRing

      prednisolone
      zithromax non prescription
      generic tadalafil
      no rx cialis
      cheap allopurinol
      prozac ssri?

      Reply
    93. @StevRing

      buy cialis
      glucophage 1000 mg
      http://cialisa1c.com
      metformin 750 mg er pcos
      generic prednisone
      prednisolone acetate for cats

      Reply
    94. @StevRing

      www.sildenafila1c.com
      what can i expect with cialis
      buy cialis
      can i take amoxicillin with prozac

      Reply
    95. @StevRing

      prednisone induced diabetes symptoms

      Reply
    96. @StevRing

      define levitra 10mg

      Reply
    97. @StevRing

      cheap cialis
      how to pronounce tadalafil

      Reply
    98. @StevRing

      viagra
      sildenafil fore sale

      Reply
    99. @StevRing

      buy advair

      Reply
    100. @StevRing

      xenical
      ol generic allopurinol
      http://www.cialisa1c.com
      does prednisone cause infections
      generic prednisolone
      zithromax mechanism of action

      Reply
    101. @StevRing

      generic suhagra

      Reply
    102. @StevRing

      cialis

      Reply
    103. @StevRing

      http://tadalafila1c.com
      cialis 20mg generic tadalafil
      buy allopurinol
      are ther generic forms of cialis that come in capsule form

      Reply
    104. @StevRing

      prednisone buying online

      Reply
    105. @StevRing

      generic mobic

      Reply
    106. @StevRing

      synthroid cost

      Reply
    107. @StevRing

      http://metformin0l1.com
      can i buy prednisone online

      Reply
    108. @StevRing

      cheap xenical
      prozac and alcohol interaction

      Reply
    109. @StevRing

      cheap cialis
      walmart propecia price
      http://www.viagrad9z.com
      buying sildenafil online
      http://allopurinol0l1.com
      buy cialis australia

      Reply
    110. @StevRing

      http://zithromax0l1.com

      Reply
    111. @StevRing

      buy cheap tadalafil soft tabs

      Reply
    112. @StevRing

      cost of acyclovir 400 mg

      Reply
    113. @StevRing

      http://prozac0l1.com
      metformin and b12 deficiency
      http://prednisolone0l1.com
      prednisolone vs prednison

      Reply
    114. @StevRing

      cheap tadalafil

      Reply
    115. @StevRing

      vardenafil hcl 20mg tab pharmacy

      Reply
    116. @StevRing

      cheap zithromax
      lisinopril effect on potassium levels and foods not to eat

      Reply
    117. @StevRing

      viagra
      lisinopril and naltrexone interaction

      Reply
    118. @StevRing

      http://viagrav3u.com

      Reply
    119. @StevRing

      cheap tadalafil
      cialis discussion forums
      buy sildenafil
      tadalafil patient copay assistance
      generic cialis
      cialis, discount in usa

      Reply
    120. @StevRing

      http://www.buylevitra.us.com

      Reply
    121. @StevRing

      skelaxin

      Reply
    122. @StevRing

      generic sildenafil
      how to boost cialis
      cheap viagra
      elitenet tadalafil powder

      Reply
    123. @StevRing

      sildenafil

      Reply
  2. seroquel high [url=http://quetiapineseroquelik.com/]quetiapine 25mg[/url] quetiapine fumarate 50 mg seroquel high seroquel dosage for sleep

    Reply
  3. trazodone for insomnia [url=http://trazodoneud.com/]is trazodone addictive[/url] trazodone alcohol trazodone effects trazodone half life

    Reply
  4. viagra online generic [url=http://sofiapharmku.com/]п»їgeneric viagra[/url] generic viagra canada canadian pharmacy generic viagra when will viagra go generic

    Reply
  5. п»їcanadian pharmacy [url=http://canadianpharmacyonlinesrvh.com/]online canadian pharmacy[/url] online canadian pharmacy canadian pharmacy online online pharmacy canada

    Reply
  6. quetiapine high [url=http://quetiapineseroquelik.com/]quetiapine warnings[/url] quetiapine fumarate side effects seroquel 25 mg can you overdose on seroquel

    Reply
  7. online viagra prescription [url=http://emmapharmsr.com/]buy real viagra online[/url] cheap viagra online buying viagra online best place to buy viagra online

    Reply