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

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Data Structure Binary Tree: Lowest Common An

系統(tǒng) 1942 0

http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/

      
         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
      
      
        bool
      
       findpath(node *root, vector<
      
        int
      
      > &path, 
      
        int
      
      
         n) {


      
      
        19
      
      
        if
      
       (!root) 
      
        return
      
      
        false
      
      
        ;


      
      
        20
      
           path.push_back(root->
      
        data);


      
      
        21
      
      
        if
      
       (root->data == n) 
      
        return
      
      
        true
      
      
        ;


      
      
        22
      
      
        if
      
       (root->left && findpath(root->left, path, n) || root->right && findpath(root->right, path, n)) 
      
        return
      
      
        true
      
      
        ;


      
      
        23
      
      
            path.pop_back();


      
      
        24
      
      
        return
      
      
        false
      
      
        ;


      
      
        25
      
      
        }


      
      
        26
      
      
        27
      
      
        int
      
       LCA(node *root, 
      
        int
      
       n1, 
      
        int
      
      
         n2) {


      
      
        28
      
      
        if
      
       (!root) 
      
        return
      
       -
      
        1
      
      
        ;


      
      
        29
      
           vector<
      
        int
      
      >
      
         path1, path2;


      
      
        30
      
      
        if
      
       (!findpath(root, path1, n1) || !findpath(root, path2, n2)) 
      
        return
      
       -
      
        1
      
      
        ;


      
      
        31
      
      
        int
      
       i = 
      
        0
      
      
        ;


      
      
        32
      
      
        for
      
       (; i < path1.size() && i < path2.size(); i++
      
        ) {


      
      
        33
      
      
        if
      
       (path1[i] != path2[i]) 
      
        break
      
      
        ;


      
      
        34
      
      
            }


      
      
        35
      
      
        return
      
       path1[i-
      
        1
      
      
        ];


      
      
        36
      
      
        }


      
      
        37
      
      
        38
      
      
        int
      
      
         main() {


      
      
        39
      
           node *root = 
      
        new
      
       node(
      
        1
      
      
        );


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


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


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


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


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


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


      
      
        46
      
           cout << 
      
        "
      
      
        LCA(4, 5) is 
      
      
        "
      
       << LCA(root, 
      
        4
      
      , 
      
        5
      
      ) <<
      
         endl;


      
      
        47
      
           cout << 
      
        "
      
      
        LCA(4, 6) is 
      
      
        "
      
       << LCA(root, 
      
        4
      
      , 
      
        6
      
      ) <<
      
         endl;


      
      
        48
      
           cout << 
      
        "
      
      
        LCA(3, 4) is 
      
      
        "
      
       << LCA(root, 
      
        3
      
      , 
      
        4
      
      ) <<
      
         endl;


      
      
        49
      
           cout << 
      
        "
      
      
        LCA(2, 4) is 
      
      
        "
      
       << LCA(root, 
      
        2
      
      , 
      
        4
      
      ) <<
      
         endl;


      
      
        50
      
      
        return
      
      
        0
      
      
        ;


      
      
        51
      
       }
    

?one traversal

      
         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
      
       node* LCA(node *root, 
      
        int
      
       n1, 
      
        int
      
      
         n2) {


      
      
        19
      
      
        if
      
       (!root) 
      
        return
      
      
         NULL;


      
      
        20
      
      
        if
      
       (root->data == n1 || root->data == n2) 
      
        return
      
      
         root;


      
      
        21
      
           node *l = LCA(root->
      
        left, n1, n2);


      
      
        22
      
           node *r = LCA(root->
      
        right, n1, n2);


      
      
        23
      
      
        if
      
       (l && r) 
      
        return
      
      
         root;


      
      
        24
      
      
        return
      
       l?
      
         l : r;


      
      
        25
      
      
        }


      
      
        26
      
      
        27
      
      
        int
      
      
         main() {


      
      
        28
      
           node *root = 
      
        new
      
       node(
      
        1
      
      
        );


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


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


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


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


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


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


      
      
        35
      
           cout << 
      
        "
      
      
        LCA(4, 5) is 
      
      
        "
      
       << LCA(root, 
      
        4
      
      , 
      
        5
      
      )->data <<
      
         endl;


      
      
        36
      
           cout << 
      
        "
      
      
        LCA(4, 6) is 
      
      
        "
      
       << LCA(root, 
      
        4
      
      , 
      
        6
      
      )->data <<
      
         endl;


      
      
        37
      
           cout << 
      
        "
      
      
        LCA(3, 4) is 
      
      
        "
      
       << LCA(root, 
      
        3
      
      , 
      
        4
      
      )->data <<
      
         endl;


      
      
        38
      
           cout << 
      
        "
      
      
        LCA(2, 4) is 
      
      
        "
      
       << LCA(root, 
      
        2
      
      , 
      
        4
      
      )->data <<
      
         endl;


      
      
        39
      
      
        return
      
      
        0
      
      
        ;


      
      
        40
      
       }
    

上面這段代碼有些問(wèn)題,當(dāng)n1或者n2不存在時(shí)應(yīng)該回NULL,但是回的是n1或者n2

      
         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
      
       node* _LCA(node *root, 
      
        int
      
       n1, 
      
        int
      
       n2, 
      
        bool
      
       &v1, 
      
        bool
      
       &
      
        v2) {


      
      
        19
      
      
        if
      
       (!root) 
      
        return
      
      
         NULL;


      
      
        20
      
      
        if
      
       (root->data ==
      
         n1) {


      
      
        21
      
               v1 = 
      
        true
      
      
        ;


      
      
        22
      
      
        return
      
      
         root;


      
      
        23
      
      
            }


      
      
        24
      
      
        if
      
       (root->data ==
      
         n2) {


      
      
        25
      
               v2 = 
      
        true
      
      
        ;


      
      
        26
      
      
        return
      
      
         root;


      
      
        27
      
      
            }


      
      
        28
      
           node *l = _LCA(root->
      
        left, n1, n2, v1, v2);


      
      
        29
      
           node *r = _LCA(root->
      
        right, n1, n2, v1, v2);


      
      
        30
      
      
        if
      
       (l && r) 
      
        return
      
      
         root;


      
      
        31
      
      
        return
      
       l?
      
         l : r;


      
      
        32
      
      
        }


      
      
        33
      
      
        34
      
      
        bool
      
       findnode(node *root, 
      
        int
      
      
         k) {


      
      
        35
      
      
        if
      
       (!root) 
      
        return
      
      
        false
      
      
        ;


      
      
        36
      
      
        return
      
       (root->data == k || findnode(root->left, k) || findnode(root->
      
        right, k));


      
      
        37
      
      
        }


      
      
        38
      
      
        39
      
       node *LCA(node *root, 
      
        int
      
       n1, 
      
        int
      
      
         n2) {


      
      
        40
      
      
        bool
      
       v1 = 
      
        false
      
      
        ;


      
      
        41
      
      
        bool
      
       v2 = 
      
        false
      
      
        ;


      
      
        42
      
           node *lca =
      
         _LCA(root, n1, n2, v1, v2);


      
      
        43
      
      
        if
      
       (v1 && v2 || v1 && findnode(root, n2) || v2 && findnode(root, n1)) 
      
        return
      
      
         lca;


      
      
        44
      
      
        return
      
      
         NULL;


      
      
        45
      
      
        }


      
      
        46
      
      
        47
      
      
        int
      
      
         main() {


      
      
        48
      
           node *root = 
      
        new
      
       node(
      
        1
      
      
        );


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


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


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


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


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


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


      
      
        55
      
           cout << 
      
        "
      
      
        LCA(4, 5) is 
      
      
        "
      
       << LCA(root, 
      
        4
      
      , 
      
        5
      
      )->data <<
      
         endl;


      
      
        56
      
           cout << 
      
        "
      
      
        LCA(4, 6) is 
      
      
        "
      
       << LCA(root, 
      
        4
      
      , 
      
        6
      
      )->data <<
      
         endl;


      
      
        57
      
           cout << 
      
        "
      
      
        LCA(3, 4) is 
      
      
        "
      
       << LCA(root, 
      
        3
      
      , 
      
        4
      
      )->data <<
      
         endl;


      
      
        58
      
           cout << 
      
        "
      
      
        LCA(2, 4) is 
      
      
        "
      
       << LCA(root, 
      
        2
      
      , 
      
        4
      
      )->data <<
      
         endl;


      
      
        59
      
      
        return
      
      
        0
      
      
        ;


      
      
        60
      
       }
    

?

Data Structure Binary Tree: Lowest Common Ancestor in a Binary Tree


更多文章、技術(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)論
主站蜘蛛池模板: 国产 欧美 日韩 一区 | 日韩欧美亚洲 | 久www | 亚洲狼人综合干 | 今井夏帆av一区二区 | 先锋av资源在线 | 噜噜噜动态图超猛烈 | xxxxhdvideosex| 96精品专区国产在线观看高清 | 国产在线毛片 | 国产日韩欧美三级 | concern超碰在线 | 久久久久成人精品 | 日本VA在线视频播放 | 亚洲综合国产一区二区三区 | 日韩免费一区二区 | 一区二区三区在线免费观看 | 大香伊蕉国产短视频69 | 久久精品小视频 | 久久精品一区 | 亚洲欧洲中文日韩久久AV乱码 | 久久久久久久久久综合情日本 | 亚洲精品一区 | 三极片免费看 | 99热久久国产综合精品久久国产 | 国产一区二区三区久久久久久久久 | 国产三级在线播放 | 人人狠狠综合88综合久久 | 欧美日本免费一区二区三区 | 三级视频全过程 | 欧美一区视频 | 五月色电影 | 欧美日韩一区二区综合在线视频 | 成人观看网站a | 欧美精品1区2区 | 色婷五月综激情亚洲综合 | 午夜羞羞| 国产成人精品午夜 | 日韩中文字幕av | 日韩国产欧美在线观看 | 色网站免费视频 |