2#include3#include4#include5#include6#include7#include8#include9u" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Data Structure Binary Tree: Print ancestors

系統 2374 0

http://www.geeksforgeeks.org/print-ancestors-of-a-given-binary-tree-node-without-recursion/

      
         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
      
       printancestor(node *root, 
      
        int
      
      
         key) {


      
      
        19
      
      
        if
      
       (!root) 
      
        return
      
      
        ;


      
      
        20
      
           stack<node*>
      
         S;


      
      
        21
      
      
        while
      
       (
      
        1
      
      
        ) {


      
      
        22
      
      
        while
      
       (root && root->data !=
      
         key) {


      
      
        23
      
      
                    S.push(root);


      
      
        24
      
                   root = root->
      
        left;


      
      
        25
      
      
                }


      
      
        26
      
      
        if
      
       (root && root->data == key) 
      
        break
      
      
        ;


      
      
        27
      
      
        if
      
       (S.top()->right ==
      
         NULL) {


      
      
        28
      
                   root =
      
         S.top();


      
      
        29
      
      
                    S.pop();


      
      
        30
      
      
        while
      
       (!S.empty() && S.top()->right ==
      
         root) {


      
      
        31
      
                       root =
      
         S.top();


      
      
        32
      
      
                        S.pop();


      
      
        33
      
      
                    }


      
      
        34
      
      
                }


      
      
        35
      
               root = S.empty()? NULL : S.top()->
      
        right;


      
      
        36
      
      
            }


      
      
        37
      
      
        while
      
       (!
      
        S.empty()) {


      
      
        38
      
               cout << S.top()->data << 
      
        "
      
      
        "
      
      
        ;


      
      
        39
      
      
                S.pop();


      
      
        40
      
      
            }


      
      
        41
      
      
        }


      
      
        42
      
      
        43
      
      
        int
      
      
         main() {


      
      
        44
      
           node *root = 
      
        new
      
       node(
      
        1
      
      
        );


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


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


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


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


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


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


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


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


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


      
      
        54
      
      
        for
      
       (
      
        int
      
       i = 
      
        1
      
      ; i < 
      
        11
      
      ; i++
      
        ) {


      
      
        55
      
      
                printancestor(root, i);


      
      
        56
      
               cout <<
      
         endl;


      
      
        57
      
      
            }


      
      
        58
      
      
        return
      
      
        0
      
      
        ;


      
      
        59
      
       }
    

?

Data Structure Binary Tree: Print ancestors of a given binary tree node without recursion


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产精品一区二区免费 | 国产毛片久久久久久国产毛片 | 国产精品99久久久久久小说 | 精品国产一区二区二三区在线观看 | 久久久精品麻豆 | 国产日韩精品一区在线观看播放 | 久久2017| 天天干天天色综合 | 四虎影视1304t | 在线视频a| 久操免费视频 | 日韩视频久久 | 成人亚洲精品一区 | 久久96国产精品久久久 | 欧美日韩专区 | 国内精品伊人久久久影院 | 亚洲欧美久久一区二区 | 欧美一级成人一区二区三区 | 久久综合成人 | 美国一级毛片片aa久久综合 | 日韩在线一区二区三区免费视频 | 免费观看成人毛片 | 久热中文字幕在线精品首页 | 四虎影视库永久在线地址 | 久久亚洲国产伦理 | 亚洲成人免费网站 | 免费的毛片网站 | 国内偷自视频区视频综合 | 欧美国产激情二区三区 | 国产精品19禁在线观看2021 | 中文字幕在线观看不卡视频 | 黄色国产视频 | 一类毛片 | 国产小视频在线观看www | 91中文字幕在线播放 | 国产主播第一页 | 免费黄色影院 | 日韩视频一区二区在线观看 | 可以直接看的毛片 | 手机在线国产精品 | 亚洲日本香蕉视频 |