1、分别采用递归和非递归的方式编写两个函数,求一棵给定二叉树中叶子节点的个数
2、返回一棵给定二叉树在中序遍历下的最后一个结点
3、假设二叉树采用链式方式存储,root为其根节点,p和q分别指向二叉树中任意两个结点,编写一个函数,返回p和q最近的共同祖先。
在第三小题中采用递归:
思想:
1.判断root是不是NULL如果root为NULL,那么就无所谓祖先节点,直接返回NULL就好了
2.如果p在左子树中,q在右子树中,那么root肯定就是最近祖先
3.如果pq都在root的左子树,那么就需要递归root的左子树,右子树同理
(在找公共祖先上,find_ancestor函数有bug,暂勿复制!!!)
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char data;
struct node *lchild,*rchild;
}bintnode;
bintnode *node,*p;
int count;
typedef struct stack{
bintnode *data[100];
int tag[100];
int top;
}seqstack;
void push(seqstack *s,bintnode *t)
{
s->data[s->top]=t;
s->top++;
}
bintnode* pop(seqstack *s)
{
if(s->top!=0)
{
s->top--;
return (s->data[s->top]);
}
e