C++實(shí)現(xiàn)LeetCode(105.由先序和中序遍歷建立二叉樹)
[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍歷建立二叉樹
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
這道題要求用先序和中序遍歷來建立二叉樹,跟之前那道 Construct Binary Tree from Inorder and Postorder Traversal 原理基本相同,針對這道題,由于先序的順序的第一個(gè)肯定是根,所以原二叉樹的根節(jié)點(diǎn)可以知道,題目中給了一個(gè)很關(guān)鍵的條件就是樹中沒有相同元素,有了這個(gè)條件就可以在中序遍歷中也定位出根節(jié)點(diǎn)的位置,并以根節(jié)點(diǎn)的位置將中序遍歷拆分為左右兩個(gè)部分,分別對其遞歸調(diào)用原函數(shù),參見代碼如下:
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;
}
};
下面來看一個(gè)例子, 某一二叉樹的中序和后序遍歷分別為:
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
做完這道題后,大多人可能會有個(gè)疑問,怎么沒有由先序和后序遍歷建立二叉樹呢,這是因?yàn)橄刃蚝秃笮虮闅v不能唯一的確定一個(gè)二叉樹,比如下面五棵樹:
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
從上面我們可以看出,對于先序遍歷都為 1 2 3 的五棵二叉樹,它們的中序遍歷都不相同,而它們的后序遍歷卻有相同的,所以只有和中序遍歷一起才能唯一的確定一棵二叉樹。但可能會有小伙伴指出,那第 889 題 Construct Binary Tree from Preorder and Postorder Traversal 不就是從先序和后序重建二叉樹么?難道博主被啪啪打臉了么?難道博主的一世英名就此毀于一旦了么?不,博主向命運(yùn)的不公說不,請仔細(xì)看那道題的要求 "Return any binary tree that matches the given preorder and postorder traversals.",是讓返回任意一棵二叉樹即可,所以這跟博主的結(jié)論并不矛盾。長舒一口氣,博主的晚節(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.由先序和中序遍歷建立二叉樹)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)由先序和中序遍歷建立二叉樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)超市商品管理系統(tǒng)最新版
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)超市商品管理系統(tǒng)最新版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
基于Qt OpenCV實(shí)現(xiàn)圖像數(shù)據(jù)采集軟件
這篇文章主要為大家詳細(xì)介紹了如何利用Qt+OpenCV實(shí)現(xiàn)圖像數(shù)據(jù)采集軟件,文中的示例代碼講解詳細(xì),對我學(xué)習(xí)或工作有一定參考價(jià)值,感興趣的可以了解一下2022-07-07
基于Qt實(shí)現(xiàn)的自定義樹結(jié)構(gòu)容器
在Qt框架中,盡管其提供了許多強(qiáng)大的容器類,但缺少一個(gè)通用的、靈活的樹結(jié)構(gòu)容器,所以本文將設(shè)計(jì)并實(shí)現(xiàn)一個(gè)可復(fù)用的自定義樹結(jié)構(gòu)容器,需要的可以參考下2024-12-12
一文學(xué)會c語言結(jié)構(gòu)體的定義和使用方法
數(shù)組是一種數(shù)據(jù)形式,其特點(diǎn)是多個(gè)相同類型的元素集合起來,結(jié)構(gòu)體是另一種重要的數(shù)據(jù)形式,特點(diǎn)是將不同類型的成員組合起來,下面這篇文章主要給大家介紹了關(guān)于c語言結(jié)構(gòu)體的定義和使用方法的相關(guān)資料,需要的朋友可以參考下2022-11-11
Qt實(shí)現(xiàn)兩個(gè)獨(dú)立窗口的信號通信
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)兩個(gè)獨(dú)立窗口的信號通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
c++調(diào)用動(dòng)態(tài)庫LNK2019和LNK1120無法解析的外部命令
本文主要介紹了c++調(diào)用動(dòng)態(tài)庫LNK2019和LNK1120無法解析的外部命令, 出現(xiàn)這個(gè)錯(cuò)誤一般都是函數(shù)只找到聲明但沒有實(shí)現(xiàn),或者是少了什么鏈接庫,下面就來解決一下2024-06-06
C++使用MySQL-Connector/C++連接MySQL出現(xiàn)LNK2019錯(cuò)誤的解決方法
這篇文章主要介紹了C++使用MySQL-Connector/C++連接MySQL出現(xiàn)LNK2019錯(cuò)誤的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

