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

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

Data Structure Binary Tree: Print ancestors

系統(tǒng) 2328 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


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日韩精品成人在线 | 色婷婷5月精品久久久久 | 日韩一区二区天海翼 | 免费精品视频 | 狠狠狠狠狠狠 | 四虎视频国产精品免费 | 国产免费私人影院永久免费 | 亚洲国内自拍愉拍20页 | 久久99精品久久久久久久野外 | 国产天天在线 | 全部免费的毛片在线看美国 | 99视频九九精品视频在线观看 | 91成人精品视频 | 精品久久久久久久一区二区手机版 | 一区二区三区在线播放视频 | 欧美一级特黄特黄毛片 | 国产精品久久毛片蜜月 | 婷婷综合色伊人阁 | 国产亚洲精品看片在线观看 | 黄色片一级毛片 | 欧美成人久久久免费播放 | 欧洲免费在线视频 | 国产啪视频1000部免费视频 | 成人午夜毛片在线看 | 国产成人a毛片在线 | 性做久久久久久久免费看 | 国内精品不卡一区二区三区 | 亚洲视频在线观看 | 美女a毛片 | 国产99页| 欧美中文在线观看 | 四虎国产精品永久地址51 | 天天躁狠狠躁狠狠躁夜夜躁 | 欧美高清性粉嫩交 | 亚洲国产欧美国产第一区二区三区 | 天天干天天草天天射 | 国产福利视频一区美女 | 老司机午夜精品99久久免费 | 91精品欧美产品免费观看 | 亚洲精品人成网线在线 | 亚洲精品久一区 |