欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)LeetCode(105.由先序和中序遍歷建立二叉樹(shù))

 更新時(shí)間:2021年07月22日 14:20:04   作者:Grandyang  
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(105.由先序和中序遍歷建立二叉樹(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍歷建立二叉樹(shù)

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]

Return the following binary tree:

    3
/ \
9  20
/  \
15   7

這道題要求用先序和中序遍歷來(lái)建立二叉樹(shù),跟之前那道 Construct Binary Tree from Inorder and Postorder Traversal 原理基本相同,針對(duì)這道題,由于先序的順序的第一個(gè)肯定是根,所以原二叉樹(shù)的根節(jié)點(diǎn)可以知道,題目中給了一個(gè)很關(guān)鍵的條件就是樹(shù)中沒(méi)有相同元素,有了這個(gè)條件就可以在中序遍歷中也定位出根節(jié)點(diǎn)的位置,并以根節(jié)點(diǎn)的位置將中序遍歷拆分為左右兩個(gè)部分,分別對(duì)其遞歸調(diào)用原函數(shù),參見(jiàn)代碼如下:

class Solution {
public:
    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
        return buildTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
    }
    TreeNode *buildTree(vector<int> &preorder, int pLeft, int pRight, vector<int> &inorder, int iLeft, int iRight) {
        if (pLeft > pRight || iLeft > iRight) return NULL;
        int i = 0;
        for (i = iLeft; i <= iRight; ++i) {
            if (preorder[pLeft] == inorder[i]) break;
        }
        TreeNode *cur = new TreeNode(preorder[pLeft]);
        cur->left = buildTree(preorder, pLeft + 1, pLeft + i - iLeft, inorder, iLeft, i - 1);
        cur->right = buildTree(preorder, pLeft + i - iLeft + 1, pRight, inorder, i + 1, iRight);
        return cur;
    }
};

下面來(lái)看一個(gè)例子, 某一二叉樹(shù)的中序和后序遍歷分別為:

Preorder:    5  4  11  8  13  9

Inorder:    11  4  5  13  8  9

5  4  11  8  13  9      =>          5

11  4  5  13  8  9                /  \

4  11     8   13  9      =>         5

11  4     13  8  9                  /  \

                             4   8

11       13    9        =>         5

11       13    9                    /  \

                             4   8

                            /    /     \

                           11    13    9

做完這道題后,大多人可能會(huì)有個(gè)疑問(wèn),怎么沒(méi)有由先序和后序遍歷建立二叉樹(shù)呢,這是因?yàn)橄刃蚝秃笮虮闅v不能唯一的確定一個(gè)二叉樹(shù),比如下面五棵樹(shù):

1      preorder:    1  2  3
/ \       inorder:       2  1  3
2 3       postorder:   2  3  1

1       preorder:     1  2  3
/       inorder:       3  2  1
2          postorder:   3  2  1
/
3

1        preorder:    1  2  3
/        inorder:      2  3  1
2       postorder:  3  2  1
\
3

       1         preorder:    1  2  3
\        inorder:      1  3  2
2      postorder:  3  2  1
/
3

       1         preorder:    1  2  3
\      inorder:      1  2  3
2      postorder:  3  2  1
\
3

從上面我們可以看出,對(duì)于先序遍歷都為 1 2 3 的五棵二叉樹(shù),它們的中序遍歷都不相同,而它們的后序遍歷卻有相同的,所以只有和中序遍歷一起才能唯一的確定一棵二叉樹(shù)。但可能會(huì)有小伙伴指出,那第 889 題 Construct Binary Tree from Preorder and Postorder Traversal 不就是從先序和后序重建二叉樹(shù)么?難道博主被啪啪打臉了么?難道博主的一世英名就此毀于一旦了么?不,博主向命運(yùn)的不公說(shuō)不,請(qǐng)仔細(xì)看那道題的要求 "Return any binary tree that matches the given preorder and postorder traversals.",是讓返回任意一棵二叉樹(shù)即可,所以這跟博主的結(jié)論并不矛盾。長(zhǎng)舒一口氣,博主的晚節(jié)保住了~

Github 同步地址:

https://github.com/grandyang/leetcode/issues/105

類似題目:

Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Preorder and Postorder Traversal

參考資料:

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/34538/My-Accepted-Java-Solution

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/34562/Sharing-my-straightforward-recursive-solution

到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(105.由先序和中序遍歷建立二叉樹(shù))的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)由先序和中序遍歷建立二叉樹(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語(yǔ)言中幾種常量的認(rèn)識(shí)和理解

    C語(yǔ)言中幾種常量的認(rèn)識(shí)和理解

    這篇文章主要為大家介紹了C語(yǔ)言常量的認(rèn)識(shí)和理解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中堆排序的分析總結(jié)

    C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中堆排序的分析總結(jié)

    堆是計(jì)算機(jī)科學(xué)中一類特殊的數(shù)據(jù)結(jié)構(gòu)的統(tǒng)稱,通常是一個(gè)可以被看做一棵完全二叉樹(shù)的數(shù)組對(duì)象。而堆排序是利用堆這種數(shù)據(jù)結(jié)構(gòu)所設(shè)計(jì)的一種排序算法。本文將通過(guò)圖片詳細(xì)介紹堆排序,需要的可以參考一下
    2022-04-04
  • libevent庫(kù)的使用--定時(shí)器的使用實(shí)例

    libevent庫(kù)的使用--定時(shí)器的使用實(shí)例

    這篇文章主要介紹了libevent庫(kù)的使用--定時(shí)器的使用實(shí)例,有需要的朋友可以參考一下
    2013-12-12
  • C++ 的類型轉(zhuǎn)換詳解

    C++ 的類型轉(zhuǎn)換詳解

    本篇文章是對(duì)C++ 類型轉(zhuǎn)換的詳細(xì)介紹,需要的朋友參考下,小編覺(jué)得這篇文章寫(xiě)的不錯(cuò),希望能夠給你帶來(lái)幫助
    2021-11-11
  • Visual Studio Code運(yùn)行程序時(shí)輸出中文成亂碼問(wèn)題及解決方法

    Visual Studio Code運(yùn)行程序時(shí)輸出中文成亂碼問(wèn)題及解決方法

    這篇文章主要介紹了解決Visual Studio Code運(yùn)行程序時(shí)輸出中文成亂碼問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Visual Studio 2019 如何新建 Win32項(xiàng)目的方法步驟

    Visual Studio 2019 如何新建 Win32項(xiàng)目的方法步驟

    這篇文章主要介紹了Visual Studio 2019 如何新建 Win32項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • C++ 二維數(shù)組傳參的四種方式

    C++ 二維數(shù)組傳參的四種方式

    C++的二維數(shù)組里面,通過(guò)用數(shù)組名傳參,傳過(guò)去后數(shù)組名會(huì)退化成一個(gè)一維數(shù)組指針,所以C++的函數(shù)參數(shù)不能像C語(yǔ)言一樣去寫(xiě),本文主要介紹了C++ 二維數(shù)組傳參的四種方式,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • C++ map用法總結(jié)(整理)

    C++ map用法總結(jié)(整理)

    這篇文章主要介紹了C++ map用法總結(jié)(整理),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • C語(yǔ)言算法練習(xí)之打魚(yú)還是曬網(wǎng)

    C語(yǔ)言算法練習(xí)之打魚(yú)還是曬網(wǎng)

    這篇文章主要該大家分享C語(yǔ)言打魚(yú)還是曬網(wǎng)的練習(xí),文章主要通過(guò)三天打魚(yú)兩天曬網(wǎng)的俗語(yǔ)提出問(wèn)題,在某一天輪到打魚(yú)還是曬網(wǎng),下面來(lái)看詳細(xì)內(nèi)容吧,需要的朋友可以參考一下
    2022-03-03
  • C語(yǔ)言實(shí)現(xiàn)共享單車管理系統(tǒng)

    C語(yǔ)言實(shí)現(xiàn)共享單車管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)共享單車管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評(píng)論