在进行算法分析时,语句总的执行次数T(n)是关于问题规模n的函数,算法的时间复杂度记作T(n)=O(f(n)).
+为进,-为出.
A:1+ 2+ 2- 3+ 3- 1- 4+ 4- 5+ 5-
B:1+ 2+ 2- 3+ 3- 4+ 4- 1- 5+ 5-
C:1+ 1- 2+ 3+ 4+ 5+ 5- 4- 3- 2-
故选D
(1)a为数字输出a————a————栈内为 空
(2)栈内无比*优先级高的符号,故*进栈————a————栈内为 *
(3)(进栈————a————栈内为 *(
(4)b为数字输出————ab————栈内为 *(
(5)由于左括号还未匹配,故+进栈————ab————栈内为 *(+
(6)c为数字输出————abc————栈内为 *(+
(7) )出现,将)进栈并将括号内的元素输出,括号消去————abc+————栈内为*
(8)栈内*优先级大于-,故*出栈,-入栈————abc+*————栈内为 -
(9)d为数字输出d————abc+*d
(10)表达式到头了,将栈内元素全部出栈————abc+*d-————栈内为 空
----------------------------------------------------------------------------------------------------------------------------
j 1 2 3 4 5 6 7 8 9 10 11 12
a[j] a b a b a a a b a b a a
next[j] 0 1 1 2 3 4 2 2 3 4 5 6
----------------------------------------------------------------------------------------------------------------------------
j 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
a[j] a b c a a b b c a b c a a b d a b
next[j] 0 1 1 1 2 2 3 1 1 2 3 4 5 6 7 1 2
----------------------------------------------------------------------------------------------------------------------------
struct Node {
struct Node *next;
int data;
};
struct stack {
struct Node *stackTop;
int size;
};
struct stack *createstack() {
struct stack *mystack = (struct stack *)malloc(sizeof(struct stack));
mystack->stackTop = NULL;
mystack->size = 0;
return mystack;
}
struct Node *createnode(int data) {
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
void inpop(struct stack *mystack, int data) {
struct Node *newnode = createnode(data);
newnode->next = mystack->stackTop;
mystack->stackTop = newnode;
mystack->size++;
}
void outpop(struct stack *mystack) {
if (mystack->size == 0) {
printf("栈为空无法出栈!\n");
system("pause");
} else {
struct Node *newnode = mystack->stackTop->next;
free(mystack->stackTop);
mystack->stackTop = newnode;
mystack->size--;
}
}int main() {
struct stack *mystack = createstack();
/*输入字符串*/
while (empty(mystack)) {
printf("%d ", top(mystack));
outpop(mystack);
}
/*输出字符串*/
printf("\n");
system("pause");
return 0;
}
比较输入与输出是否相等即可
int maxDepth(TreeNode* root)
{
if(root==NULL)
return 0;
int left=maxDepth(root->left);
int right=maxDepth(root->right);
return left >right ? left+1 : right+1;
}