【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.
有了先序和中序,基本思路就是遞迴的找左右子樹的根節點,加入到樹中
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *root = NULL; // 儲存樹的根節點
// 遞迴函式,pre是先序,pl和pr為左或右子樹的區間,in為中序,il和ir同為左或右子樹的區間,sub決定是左子樹還是右子樹,par為父親結點
void fun(vector<int> &pre, int pl, int pr, vector<int> &in, int il, int ir, int sub, TreeNode *par) {
if (pl <= pr) { // 左或右子樹不為空
TreeNode *tmp = new TreeNode(pre[pl]); // 新建一個結點,即某個子樹的根節點
if (root == NULL) {
root = tmp; // 根節點
par = root;
} else {
// 根據sub判斷新增到左子樹還是右子樹
if (sub == 0) par->left = tmp;
if (sub == 1) par->right = tmp;
}
// 找到根節點在中序遍歷中的位置
int ipos = il;
for (int i = il; i <= ir; ++i) {
if (in[i] == pre[pl]) {
ipos = i;
break;
}
}
// 找到先序遍歷中左右子樹的分隔位置
int ppos = pl + ipos - il;
// 遞迴構造左右子樹
if (ipos > il) fun(pre, pl+1, ppos, in, il, ipos-1, 0, tmp); // 有左子樹
if (ipos < ir) fun(pre, ppos+1, pr, in, ipos+1, ir, 1, tmp); // 有右子樹
}
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.size() == 0) return NULL;
//TreeNode *root = NULL;
fun(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1, 0, NULL);
return root;
}
};
相關推薦
【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 tha
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 exam
演算法設計與分析課作業【week3】leetode--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 t
leetcode: 105. Construct Binary Tree from Preorder and Inorder Traversal
Difficulty Medium. Problem Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicate
#Leetcode# 105. Construct Binary Tree from Preorder and Inorder Traversal
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ Given preorder and inorder traversal of a tree, construct
LeetCode:M-105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. /** * Definition for a binary tree node. * public class Tr
105. Construct Binary Tree from Preorder and Inorder Traversal
col eno span nod def hal 遞歸 solution 中序 Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume th
105 Construct Binary Tree from Preorder and Inorder Traversal 從前序與中序遍歷序列構造二叉樹
leet blog pub struct class null true ems inorder 給定一棵樹的前序遍歷與中序遍歷,依據此構造二叉樹。註意:你可以假設樹中沒有重復的元素。例如,給出前序遍歷 = [3,9,20,15,7]中序遍歷 = [9,3,15,20,7]
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, gi
105. Construct Binary Tree from Preorder and Inorder Traversal - Medium
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, giv
樹之105 Construct Binary Tree from Preorder and Inorder Traversal
題目連結:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 參考連結:https://blog.csdn.net/qq_36172443/article/details/81
leetcode-105- 從前序與中序遍歷序列構造二叉樹(construct binary tree from preorder and inorder traversal)-java
題目及測試 package pid105; /*從前序與中序遍歷序列構造二叉樹 根據一棵樹的前序遍歷與中序遍歷構造二叉樹。 注意: 你可以假設樹中沒有重複的元素。 例如,給出 前序遍歷 preorder = [3,9,20,15,7] 中序遍歷 inorder = [9,3,15
leetcode 105. 從前序與中序遍歷序列構造二叉樹(Construct Binary Tree from Preorder and Inorder Traversal)
cpp 根據 描述 沒有 etc pre flow fir cout 目錄 題目描述: 示例: 解法: 題目描述: 根據一棵樹的前序遍歷與中序遍歷構造二叉樹。
Leetcode:Construct Binary Tree from Preorder and Inorder Traversal
ide 訪問 false 討論 遍歷 使用 int 叠代 技術 題目大意:分別按先序和中序遍歷同一個n結點二叉樹,得到兩個結點數組P和I。要求利用這些結點數組還原二叉樹。 這道題考驗對二叉樹的理解。先說明一些基礎的知識: 先序遍歷表示當訪問一個結點時,先訪問結
LeetCode - Construct Binary Tree from Preorder and Inorder Traversal
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) :
Construct Binary Tree from Preorder and Inorder Traversal -- LeetCode
原題連結: http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 這道題是樹中比較有難度的題目。須要依據先序遍歷和中序
[LeetCode] 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. 這道題要求用先序和中序遍
LeetCode刷題Medium篇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
Leetcode105 Construct Binary Tree from Preorder and Inorder Traversal Java實現
提高 bre turn ava mage rsa shm [] img 先寫了一個最原始的方法1:(java沒有切片很難受啊) /** * Definition for a binary tree node. * public class TreeNode { *
[Swift]LeetCode105. 從前序與中序遍歷序列構造二叉樹 | Construct Binary Tree from Preorder and Inorder Traversal
eno restart pub n) spa win int end als Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume th