由于岗位实践需要,故写了一个树的基本操作,包括先,中,后序递归和非递归,计算高度,计算左子树个数,无其他用处,警示自己而已 ..
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stack>
using namespace std;
// tree node
typedef struct TreeNode {
char data;
struct TreeNode* lChild;
struct TreeNode* rChild;
} Node;
int n;
stack<Node*> s;
// 先序 create tree
void createTree(Node **t, char *c)
{
n++;
if(n > strlen(c) - 1) return;
if(c[n] == '0') *t = NULL;
else
{
(*t) = (Node*)malloc(sizeof(Node));
(*t)->data = c[n];
createTree(&(*t)->lChild, c);
createTree(&(*t)->rChild, c);
}
}
// 先序递归访问
void preVisit(Node *t)
{