07 reconstruction binary tree
description
And enter the result in a preorder traversal of a binary tree in preorder traversal of the binary tree rebuild. Suppose Results preorder traversal order and input of duplicate numbers are free.
For example, given
Preorder traversal preorder = [3,9,20,15,7]
preorder inorder = [9,3,15,20,7]Returns the following binary tree:
3
/ \
9 20
/ \
15 7
limit:
0 <= the number of nodes <= 5000
Thinking
Previous root node is found in the order of sequence, with the nodes in order to divide the left and right trees tree. And then the left tree and right tree recursive processing.
Time complexity: T (n) = T (n-1) + O (n) time to find a node recursively, the need to find an index value O (n).
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.empty()||inorder.empty()){
return nullptr;
}
int root_inorde_index=0;
for (;root_inorde_index < inorder.size() ;root_inorde_index++)
{
if(inorder[root_inorde_index]==preorder[0]){
break;
}
}
TreeNode *root= new TreeNode(preorder[0]);
vector<int> pre_left,pre_right,in_left,in_right;
for (size_t i = 0; i <root_inorde_index; i++)
{
pre_left.push_back(preorder[i+1]);
in_left.push_back(inorder[i]);
}
for (size_t i = root_inorde_index+1; i < preorder.size(); i++)
{
pre_right.push_back(preorder[i]);
in_right.push_back(inorder[i]);
}
root->left=buildTree(pre_left,in_left);
root->right=buildTree(pre_right,in_right);
return root;
}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
return BinaryTree(preorder, 0, preorder.size(), inorder, 0, inorder.size());
}
TreeNode* BinaryTree(vector<int> preorder, int pre_begin, int pre_end, vector<int> inorder, int in_begin, int in_end){
if(pre_begin>=pre_end||in_begin>=in_end){
return nullptr;
}
int p_in=in_begin;
for (; p_in < in_end; p_in++)
{
if(inorder[p_in]==preorder[pre_begin]){
break;
}
}
TreeNode* root = new TreeNode(preorder[pre_begin]);
root->left= BinaryTree(preorder, pre_begin+1, pre_begin+1+(p_in-in_begin), inorder, in_begin, p_in);
root->right= BinaryTree(preorder, pre_begin+1+(p_in-in_begin), pre_end, inorder, p_in+1, in_end);
return root;
}
};
09 with two stacks queue
topic
A queue is implemented with two stacks. The following statement queue, implement its two functions appendTail and deleteHead, were completed at the end of the queue insert and delete integer integer in the head of the queue function. (If there is no queue element, deleteHead operation returns -1)
Example 1:
输入:
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出:[null,null,3,-1]Example 2:
输入:
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出:[null,-1,null,null,5,2]prompt:
1 <= values <= 10000
will most appendTail, deleteHead were 10,000 calls
Thinking
As long as the queue is a push operation, it will push data into pushStack stack.
To achieve the queue pop operation, there are two points principles,
If it is empty will popStack pushStack popStack all the elements in place, the top element is then taken popStack HOL queue;
If popStack is not empty, then we just get the top element popStack.
class CQueue {
public:
stack<int> push_stack;
stack<int> pop_stack ;
CQueue() {
}
void appendTail(int value) {
push_stack.push(value);
}
int deleteHead() {
if (push_stack.empty() && pop_stack.empty())
return -1;
if(pop_stack.size()<=0){
while (!push_stack.empty())
{
pop_stack.push(push_stack.top());
push_stack.pop();
}
}
int temp=pop_stack.top();
pop_stack.pop();
return temp;
}
};