递归求指定结点node在以root为根结点的二叉树中的路径,并保存在栈s中。
#include <stdio.h>
#include <stdlib.h>
#define Stack_Size 50
typedef BiTNode* ElemType;
typedef struct{
ElemType elem[Stack_Size];
int top;
}Stack;
void init_stack(Stack *S); // 初始化栈
bool push(Stack* S, ElemType x); //x 入栈
bool pop(Stack* S, ElemType *px); //出栈,元素保存到px所指的单元,函数返回true,栈为空时返回 false
bool top(Stack* S, ElemType *px); //获取栈顶元素,将其保存到px所指的单元,函数返回true,栈满时返回 false
bool is_empty(Stack* S); // 栈为空时返回 true,否则返回 false
typedef int DataType;
typedef struct Node{
DataType data;
struct Node* left;
struct Node* right;
}BiTNode, *BiTree;
bool path(BiTNode* root, BiTNode* node, Stack* s)
{
if (root == NULL) {
return false;
}
push(s, root);
if (root == node) {
return true;
}
BiTNode* bin;
if (path(root->left, node, s)) {
return true;
}
if (path(root->right, node, s)) {
return true;
}
pop(s, &bin);
return false;
}