classMyQueue{public:/** Initialize your data structure here. */MyQueue(){}/** Push element x to the back of queue. */voidpush(int x){left.push(x);}/** Removes the element from in front of queue and returns that element. */intpop(){if(right.empty()){while(!left.empty()){int top = left.top();left.pop();right.push(top);}}int top = right.top();right.pop();return top;}/** Get the front element. */intpeek(){if(right.empty()){while(!left.empty()){int top = left.top();left.pop();right.push(top);}}int top = right.top();return top;}/** Returns whether the queue is empty. */boolempty(){return left.empty()&& right.empty();}stack<int>left;stack<int>right;};/*** Your MyQueue object will be instantiated and called as such:* MyQueue* obj = new MyQueue();* obj->push(x);* int param_2 = obj->pop();* int param_3 = obj->peek();* bool param_4 = obj->empty();*/
再写循环队列 class MyCircularQueue {
public:/** Initialize your data structure here. Set the size of the queue to be k. */MyCircularQueue(int k) {array (int *)malloc(sizeof(int)*k);capacity k;size 0;front 0;rear 0;}/** Insert an element into the circu…
前序遍历 /*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*//*** Note: The returned array must be malloced, assume caller calls free().*/
int *array;
int size;void _preorde…
平衡二叉树 /*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/int MAX(int a,int b)
{return a > b ? a : b;
}//求高度
int getHeight(struct TreeNode *root){if(root NULL){…
前序中序遍历构造二叉树 思路
在前序中找根结点根据根结点 中序,分成左右两棵子树根据子树长度,把前序分成左右两颗子树递归处理子树
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* s…