2#include3#include4#include5#include6#include7#include8#include9usingnamespacestd;1011structnode{" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Data Structure Binary Tree: Morris traversal

系統(tǒng) 2124 0

http://www.geeksforgeeks.org/morris-traversal-for-preorder/

      
         1
      
       #include <iostream>


      
         2
      
       #include <vector>


      
         3
      
       #include <algorithm>


      
         4
      
       #include <queue>


      
         5
      
       #include <stack>


      
         6
      
       #include <
      
        string
      
      >


      
         7
      
       #include <fstream>


      
         8
      
       #include <map>


      
         9
      
      
        using
      
      
        namespace
      
      
         std;


      
      
        10
      
      
        11
      
      
        struct
      
      
         node {


      
      
        12
      
      
        int
      
      
         data;


      
      
        13
      
      
        struct
      
       node *left, *
      
        right;


      
      
        14
      
           node() : data(
      
        0
      
      
        ), left(NULL), right(NULL) { }


      
      
        15
      
           node(
      
        int
      
      
         d) : data(d), left(NULL), right(NULL) { }


      
      
        16
      
      
        };


      
      
        17
      
      
        18
      
      
        void
      
       prints(node *
      
        root) {


      
      
        19
      
      
        while
      
      
         (root) {


      
      
        20
      
      
        if
      
       (root->left ==
      
         NULL) {


      
      
        21
      
                   cout << root->data << 
      
        "
      
      
        "
      
      
        ;


      
      
        22
      
                   root = root->
      
        right;


      
      
        23
      
      
                }


      
      
        24
      
      
        else
      
      
         {


      
      
        25
      
                   node* cur = root->
      
        left;


      
      
        26
      
      
        while
      
       (cur->right && cur->right != root) cur = cur->
      
        right;


      
      
        27
      
      
        if
      
       (cur->right ==
      
         NULL) {


      
      
        28
      
                       cout << root->data << 
      
        "
      
      
        "
      
      
        ;


      
      
        29
      
                       cur->right =
      
         root;


      
      
        30
      
                       root = root->
      
        left;


      
      
        31
      
      
                    }


      
      
        32
      
      
        else
      
      
         {


      
      
        33
      
                       cur->right =
      
         NULL;


      
      
        34
      
                       root = root->
      
        right;


      
      
        35
      
      
                    }


      
      
        36
      
      
                }


      
      
        37
      
      
            }


      
      
        38
      
      
        }


      
      
        39
      
      
        40
      
      
        int
      
      
         main() {


      
      
        41
      
           node *root = 
      
        new
      
       node(
      
        1
      
      
        );


      
      
        42
      
           root->left = 
      
        new
      
       node(
      
        2
      
      
        );


      
      
        43
      
           root->right = 
      
        new
      
       node(
      
        3
      
      
        );


      
      
        44
      
           root->left->left = 
      
        new
      
       node(
      
        4
      
      
        );


      
      
        45
      
           root->left->right = 
      
        new
      
       node(
      
        5
      
      
        );


      
      
        46
      
           root->right->left = 
      
        new
      
       node(
      
        6
      
      
        );


      
      
        47
      
           root->right->right = 
      
        new
      
       node(
      
        7
      
      
        );


      
      
        48
      
           root->left->left->left = 
      
        new
      
       node(
      
        8
      
      
        );


      
      
        49
      
           root->left->left->right = 
      
        new
      
       node(
      
        9
      
      
        );


      
      
        50
      
           root->left->right->left = 
      
        new
      
       node(
      
        10
      
      
        );


      
      
        51
      
           root->left->right->right = 
      
        new
      
       node(
      
        11
      
      
        );


      
      
        52
      
      
            prints(root);


      
      
        53
      
      
        return
      
      
        0
      
      
        ;


      
      
        54
      
       }
    

?

Data Structure Binary Tree: Morris traversal for Preorder


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 登封市| 和顺县| 新疆| 临朐县| 曲沃县| 宁河县| 游戏| 淳化县| 瑞丽市| 九江县| 汶上县| 西城区| 淅川县| 阜南县| 新绛县| 靖江市| 门源| 秦皇岛市| 扶风县| 承德县| 宜黄县| 平阴县| 孟津县| 莒南县| 怀远县| 红原县| 霍城县| 瓦房店市| 济南市| 西乌珠穆沁旗| 湖北省| 永顺县| 南阳市| 方正县| 化德县| 贵德县| 泸水县| 长子县| 乃东县| 沂源县| 泾阳县|