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. 由preorder得到根结点;把preorder第一个点删掉;

2. 先建左子树;再建右子树;

通过一个区间来表示左右子树的取值范围。因为inorder左右子树的范围都是连续的。中间就是root。


 1 class Solution {
2 public:
3 TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
4 return recursive(preorder, inorder, 0, inorder.size() - 1);
5 }
6
7 TreeNode* recursive(vector<int> &preorder, vector<int> &inorder, int s, int e) {
8 if (s > e) return NULL;
9 if (preorder.empty()) return NULL;
10 TreeNode *root = new TreeNode(preorder.front());
11 preorder.erase(preorder.begin());
12
13 int i = s;
14 for (; i <= e && inorder[i] != root->val; ++i);
15 root->left = recursive(preorder, inorder, s, i - 1);
16 root->right = recursive(preorder, inorder, i + 1, e);
17 }
18 };

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. postorder,最后一个元素是根结点。

2. 先构建右子树,再构建左子树。


 1 class Solution {
2 public:
3 TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
4 return recursive(postorder, inorder, 0, inorder.size() - 1);
5 }
6
7 TreeNode* recursive(vector<int> &postorder, vector<int> &inorder, int s, int e) {
8 if (s > e) return NULL;
9 if (postorder.empty()) return NULL;
10 TreeNode *root = new TreeNode(postorder.back());
11 postorder.pop_back();
12
13 int i = s;
14 for (; i <= e && inorder[i] != root->val; ++i);
15 root->right = recursive(postorder, inorder, i + 1, e);
16 root->left = recursive(postorder, inorder, s, i - 1);
17 }
18 };

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

时间: 2024-10-12 14:07:10

Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal的相关文章

[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 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

105. Construct Binary Tree from Inorder and preorder Traversal

/* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root一定在中间那个点,还是要找一下中间点的位置 * p.left = construct(preorder, preStart + 1, preStart + (k - inStart),inorder, inStart, k - 1); * p.right = construct(preorder,

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 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 – Refresh – Construct Binary Tree from Inorder and Preorder Traversal

Only different with preorder and postorder is that the root start from the beginning for preorder. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NUL

[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. 这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见我之前的一篇博客Convert Sorted Array to Bin

[Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

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,2,3,4,5,6,#}的后序遍历为4->5->2->6-&

[Leetcode] Construct Binary Tree from Inorder and Postorder Traversal I,II

这两个问题实际上是同一个问题,需要对三种遍历方式的规律非常清楚. 对于前序遍历,第一个元素实际上就是root,然后后面的元素前半部分是左树的node,后半部分是右树的node 对于中序遍历,一旦我们知道了root节点,那么就可以将其分为两半部分,也就是左树和右树 对于后序遍历,我们可以缺点最后一个节点是root节点,剩下的部分:前半部分是左树节点,后半部分是右树节点. 这样使用递归就可以解决,需要使用两组index划定两种遍历的range,当left>right的时候,就需要返回null 有点类