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(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode *getTree(vector<int> &iv, vector<int> &pv, int ist, int ied, int pst, int ped) {
13         if (ist > ied) return NULL;
14         int current = -1, len = 0;
15         for (int i = ist; i <= ied; i++) {
16             if (pv[pst] == iv[i]) {
17                 current = i;
18                 break;
19             }
20         }
21         len = current - ist;
22         TreeNode *root = new TreeNode(pv[pst]);
23         root->left = getTree(iv, pv, ist, current-1, pst+1, pst+len);
24         root->right = getTree(iv, pv, current+1, ied, pst+len+1, ped);
25     }
26
27     TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
28         if (inorder.size() == 0 || inorder.size() != preorder.size()) return NULL;
29         return getTree(inorder, preorder, 0, inorder.size()-1, 0, preorder.size()-1);
30     }
31 };
时间: 2024-10-07 05:06:12

LeetCode – Refresh – Construct Binary Tree from Inorder and Preorder Traversal的相关文章

LeetCode – Refresh – Construct Binary Tree from Inorder and Postorder Traversal

For this problem just need to know the structure of two traversal. 1. It is hard to find the root node in the inorder traversal but it is easy in postorder. The last one in post order is the root. 2. At same time, you can not figure out what is the b

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  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 *buildTr

LeetCode | 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树【Python】

LeetCode 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树[Medium][Python][二叉树][递归] Problem LeetCode Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not

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 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 dfs Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 14363 Total Submissions: 54254My Submissions Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in t

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: 115870 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp;amp; Construct Binary Tree f

1.  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 *buildTr

LeetCode 106. 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. 题目标签:Array, Tree 这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order.因为post-order是最后一个数字是root,所以要从右向左的遍历.还需要把helpe