Construct Binary Tree

105. Construct Binary Tree from Preorder and Inorder Traversal

题目链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/#/description

题目大意:给定一棵树的先序和中序遍历,要求构造出该二叉树。

题目思路:由先序遍历的特性知道,先序遍历的首元素即为根节点元素,根据中序遍历的特性知道,根节点元素将中序遍历结果划分为左子树和右子树,即根元素以左的部分为左子树的中序遍历结果,根元素以右的部分为右子树的中序遍历结果,根据中序遍历的左右子树划分结果(左右子树的节点个数),又可以将先序遍历结果划分为左右子树,从而递归可以得到整个二叉树。

算法复杂度:时间复杂度O(n),空间复杂度O(log(n)),n为节点个数

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
13         if (preorder.size() == 0 || inorder.size() == 0)
14             return nullptr;
15         return recursiveBuildTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
16     }
17     TreeNode* recursiveBuildTree(vector<int>& preorder, int pbegin, int pend, vector<int>& inorder, int ibegin, int iend) {
18         if (pbegin > pend)
19             return nullptr;
20         TreeNode* root = new TreeNode(preorder[pbegin]);
21         if (pbegin == pend)
22             return root;
23         for (int i = ibegin, j = 0; i <= iend; ++i, ++j)
24             if (inorder[i] == preorder[pbegin]) {
25                 root->left = recursiveBuildTree(preorder, pbegin + 1, pbegin + j, inorder, ibegin, i - 1);
26                 root->right = recursiveBuildTree(preorder, pbegin + j + 1, pend, inorder, i + 1, iend);
27                 break;
28             }
29         return root;
30     }
31 };

评测系统上运行结果:

106. Construct Binary Tree from Inorder and Postorder Traversal

题目链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/#/description

题目大意:给定一棵树的中序和后序遍历,要求构造出该二叉树。

题目思路:由后序遍历的特性知道,后序遍历的尾元素即为根节点元素,根据中序遍历的特性知道,根节点元素将中序遍历结果划分为左子树和右子树,即根元素以左的部分为左子树的中序遍历结果,根元素以右的部分为右子树的中序遍历结果,根据中序遍历的左右子树划分结果(左右子树的节点个数),又可以将后序遍历结果划分为左右子树,从而递归可以得到整个二叉树。

算法复杂度:时间复杂度O(n),空间复杂度O(log(n)),n为节点个数

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
13         if (inorder.empty())
14             return nullptr;
15         return recurBuildTree(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
16     }
17 private:
18     TreeNode* recurBuildTree(vector<int>& inorder, int ibeg, int iend, vector<int>& postorder, int pbeg, int pend) {
19         if (ibeg > iend)
20             return nullptr;
21         auto root = new TreeNode(postorder[pend]);
22         for (int i = ibeg, j = 0; i <= iend; ++i, ++j) {
23             if (inorder[i] == postorder[pend]) {
24                 root->left = recurBuildTree(inorder, ibeg, i - 1, postorder, pbeg, pbeg + j - 1);
25                 root->right = recurBuildTree(inorder, i + 1, iend, postorder, pbeg + j, pend - 1);
26                 break;
27             }
28         }
29         return root;
30     }
31 };

评测系统上运行结果:

时间: 2024-10-12 11:27:43

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. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

[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. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

44: Construct Binary Tree from Inorder and Postorder Traversal

/************************************************************************/            /*       44:  Construct Binary Tree from Inorder and Postorder Traversal                            */            /*************************************************

43: Construct Binary Tree from Preorder and Inorder Traversal

/************************************************************************/            /*       43:  Construct Binary Tree from Preorder and Inorder Traversal                            */            /**************************************************

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

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 刷题之路 64 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. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po

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. 二. 题目分析 这道题和Construct Binary Tree from Preorder and Inorder Traversal类似,都是考察基本概念的,后序遍历是先遍历左子树,然后遍历右子树,最