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 };

评测系统上运行结果:

 

posted @ 2017-04-22 07:35  小谷子的博客园  阅读(201)  评论(0编辑  收藏  举报