C++實(shí)現(xiàn)LeetCode(105.由先序和中序遍歷建立二叉樹(shù))
[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/
到此這篇關(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ǔ)言數(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-04libevent庫(kù)的使用--定時(shí)器的使用實(shí)例
這篇文章主要介紹了libevent庫(kù)的使用--定時(shí)器的使用實(shí)例,有需要的朋友可以參考一下2013-12-12Visual Studio Code運(yùn)行程序時(shí)輸出中文成亂碼問(wèn)題及解決方法
這篇文章主要介紹了解決Visual Studio Code運(yùn)行程序時(shí)輸出中文成亂碼問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Visual 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-03C語(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-03C語(yǔ)言實(shí)現(xiàn)共享單車管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)共享單車管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08