1/* ***********************************************
2Author :111qqz
3Created Time :2017年04月05日 星期三 16时49分57秒
4File Name :106.cpp
5************************************************ */
6/**
7 * Definition for a binary tree node.
8 * struct TreeNode {
9 * int val;
10 * TreeNode *left;
11 * TreeNode *right;
12 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
13 * };
14 */
15class Solution {
16public:
17 TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
18 int siz = inorder.size();
19 if (siz==0) return NULL;
20 int rt = postorder[siz-1];
21 int pos = -1;
22 for ( int i = 0 ; i < siz; i++)
23 {
24 if (inorder[i]==rt)
25 {
26 pos = i ;
27 break;
28 }
29 }
30 TreeNode *head = new TreeNode(rt);
31 vector<int>in,post;
32 for ( int i = 0 ; i < pos ; i++)
33 {
34 in.push_back(inorder[i]);
35 post.push_back(postorder[i]);
36 }
37 head->left = buildTree(in,post);
38 in.clear();
39 post.clear();
40 for ( int i = pos + 1 ; i < siz ; i++)
41 {
42 in.push_back(inorder[i]);
43 post.push_back(postorder[i-1]);
44 }
45 head->right = buildTree(in,post);
46 return head;
47 }
48};106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构建二叉树)
相关文章
leetcode 287. Find the Duplicate Number (floyd判圈算法找重复元素)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
leetcode 532. K-diff Pairs in an Array (找差为k的数对)
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
leetcode 448. Find All Numbers Disappeared in an Array(寻找所有消失的元素)
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
leetcode 74. Search a 2D Matrix
题目链接
题意:给一个二维数组。。。每一行每一列都分别递增。。问某个value是否出现过。。。