Leetcode:Construct Binary Tree 前序和中序、后序和中序构建二叉树

前序和中序构建二叉树

后序和中序构建二叉树

分析:主要思路就是 在中序中找根节点然后划分左右子树,具体如下:

1. 查找根节点。 我们知道前序序列的第一个元素 和 后序序列的最后一个元素 肯定是根节点,我们就以此为突破口

2. 确定根节点的坐标。 我们在 中序序列中找到 根节点 的下标。

3. 分割左右子树。

对于中序序列:根节点下标之前的都属于左子树,之后的都属于右子树

对于先序序列:根节点之后的 一段元素(该段长度可以由中序序列的左子树长度确定)属于左子树,左子树之后的元素属于右子树

对于先序序列:根节点之前的 一段元素(该段长度可以由中序序列的右子树长度确定)属于右子树,右子树之后的元素属于左子树

4.对分割好的左右子树递归构建二叉树,注意确定好每段序列的下标范围

例如:

preorder = {7,10,4,3,1,2,8,11}
inorder = {4,10,3,1,7,11,8,2}

第一步:查找根节点,对于先序而言,根节点就是第一个元素,也就是7

第二步:确定根节点的坐标,我们在 中序中查找根节点7,确定下标为4

第三步:分割左右子树。

对于中序序列:左子树为 {4, 10, 3, 1}, 右子树为{11, 8, 2}

对于先序序列:左子树为 {10, 4, 3, 1}, 右子树为{2, 8, 11}

注意:任何时候中序序列和先序序列的长度总是相等的。


class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return constructTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
}

TreeNode* constructTree(const vector<int>& preorder, int preFirst, int preLast,
const vector<int>& inorder, int inFirst, int inLast) {
int preLen = preLast - preFirst + 1;
int inLen = inLast - inFirst + 1;
assert(preLen == inLen && preLen >= 0);
if (preLen == 0) return nullptr;

TreeNode* root = new TreeNode(preorder.at(preFirst));
int rootIndex = distance(inorder.begin(), find(inorder.begin(), inorder.end(), preorder.at(preFirst)));
int leftLen = rootIndex - inFirst;
int rightLen = inLast - rootIndex;
root->left = constructTree(preorder, preFirst + 1, preFirst + leftLen, inorder, inFirst, rootIndex - 1);
root->right = constructTree(preorder, preLast - rightLen + 1, preLast, inorder, rootIndex + 1, inLast);
return root;
}
};


class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return constructTree(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
}
TreeNode* constructTree(const vector<int>& inorder, int inFirst, int inLast,
const vector<int>& postorder, int postFirst, int postLast) {
int postLen = postLast - postFirst + 1;
int inLen = inLast - inFirst + 1;
assert(postLen == inLen && postLen >= 0);
if (postLen == 0) return nullptr;

TreeNode* root = new TreeNode(postorder.at(postLast));
int rootIndex = distance(inorder.begin(), find(inorder.begin(), inorder.end(), postorder.at(postLast)));
int leftLen = rootIndex - inFirst;
int rightLen = inLast - rootIndex;
root->left = constructTree(inorder, inFirst, rootIndex - 1, postorder, postFirst, postFirst + leftLen - 1);
root->right = constructTree(inorder, rootIndex + 1, inLast, postorder, postLast - rightLen, postLast - 1);
return root;
}
};

Leetcode:Construct Binary Tree 前序和中序、后序和中序构建二叉树

时间: 2024-11-04 05:49:39

Leetcode:Construct Binary Tree 前序和中序、后序和中序构建二叉树的相关文章

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

[leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是roo

[leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根据二叉树的先序遍历和中序遍历恢复二叉树. 解题思路:可以参照 http://www.cnblogs.com/zuoyuan/p/3720138.html 的思路.递归进行解决. 代码: # Definition for a binary tree node # class TreeNode: # d

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal

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. 递归构建. 思路就是: preorder可以定位到根结点,inorder可以定位左右子树的取值范围. 1. 由

[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. 这道题要求用先序和中序遍历来建立二叉树,跟之前那道Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树原理基本相同,针对这道题,由于先

Leetcode, construct binary tree from inorder and post order traversal

Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs about this problems: 1. http://siddontang.gitbooks.io/leetcode-solution/content/tree/construct_binary_tree.html 2.http://blog.csdn.net/linhuanmars/artic

LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search SOLUTION 1: 使

LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

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. SOLUTION 1: 1. Find the root node from the preorder.(it