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

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

Data Structure Binary Tree: Print ancestors

系統 2107 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视频 | 午夜主播国产福利视频在线 | 国产精品久久久久久久久99热 | 福利视频午夜 | 亚洲成人在线免费观看 | 久久久久久岛国免费网站 | 久久天天操 | 国产爆操 | 亚洲国产成人成上人色 | 欧美性白人顶级hd | 亚洲天堂一区二区三区四区 | 日韩欧美视频一区二区三区 | 亚洲在线观看视频 | 精品一区二区日本高清 | 亚洲国产一区二区a毛片 | 色的综合 | 婷婷亚洲五月色综合 | 日本3p视频在线看高清 | 亚洲精品久久九九精品 | 久久香蕉综合精品国产 | 色天天综合久久久久综合片 | 日本一级α一片免费视频 | 真实国产乱弄免费视频 | 亚州在线播放 | 伊人影院在线观看视频 | xo欧美性另类 | 亚洲天堂福利视频 | 日本欧美成 | 欧美精品一区二区精品久久 | 免费视频久久看 | 欧美videossex精品4k | 在线成人播放毛片 | 一级片手机在线观看 | 免费毛片一级 | 99re热视频在线 | 国产高清在线视频一区二区三区 | 天天拍拍天天爽免费视频 | 亚洲国产成人久久午夜 |