求树中结点个数
int Size2(Node *root) {
if (root == NULL) { return 0; }
else if (root->left == NULL && root->right == NULL) { return 1; }
else {
int left = Size2(root->left);
int right = Size2(root->right);
return left + right + 1;
}
}
求叶子结点
int LeafSize(Node *root) {
if (root == NULL) { return 0; }
else if (root->left == NULL && root->right == NULL) { return 1; }
else {
int left = LeafSize(root->left);
int right = LeafSize(root->right);
return left + right ;
}
}
求二叉树高度
int Height(Node *root) {
if (root == NULL) { return 0; }
else if (root->left == NULL && root->right == NULL) { return 1; }
else {
if (root->left != NULL || root->right != NULL) {
int left = Height(root->left);
int right = Height(root->right);
return left < right ? (right + 1) : (left + 1);
}
}
}
求二叉树第K层结点个数
int KLevelSize(Node *root, int k) {
if (root == NULL) { return 0; }
if (k == 1) { return 1; }
int left = KLevelSize(root->left,k-1);
int right = KLevelSize(root->right,k-1);
return left + right;
}
查找含有value值的结点地址
Node* Find(Node* root,int v) {
if (root == NULL)
{ return NULL; }
if (root->value == v)
{ return root; }
Node *result = Find(root->left, v);
if (result != NULL)
{ return result; }
result = Find(root->right, v);
if (result != NULL)
{ return result; }
return NULL;
}
二叉树的前序遍历,返回一个给定值的数组
int *array;
int size;
void Preorder(Node *root)
{
if (root == NULL) {
return;
}
//printf("%d", root->value);
array[size++] = root->value;
Preorder(root->left);
Preorder(root->right);
}
int* preorderTraversal(Node *root,int* returnSize) {
array = (int*)malloc(sizeof(int) * 100 * 10000);
size = 0;
Preorder(root);
*returnSize = size;
return array;
}
检查两个树是否相同
bool isSameTree(Node *root1, Node *root2) {
if (root1 == NULL && root2 == NULL) { return true; }
if (root1 == NULL || root2 == NULL) { return false; }
return root1->value == root2->value
&& isSameTree(root1->left, root2->left)
&& isSameTree(root1->right, root2->right);
}
两棵树是否镜像对称
bool isMirror(Node *root1,Node *root2) {
if (root1 == NULL && root2 == NULL) { return true; }
if (root1 == NULL || root2 == NULL) { return false; }
return root1->value == root2->value
&& isSameTree(root1->left, root2->right)
&& isSameTree(root1->right, root2->left);
}
一个树是否是另一个树的子树
bool Find1(Node *r, Node *t){
if (r == NULL) { return false; }
if (isSameTree(r, t) == true) { return true; }
if (Find1(r->left, t) == true) { return true; }
return Find1(r->right, t);
}
bool isSubtree(Node *s, Node *t)
{ return Find1(s, t); }
判断一个树是否为平衡二叉树(每个节点的左右子树的高度差的绝对值不超过1)
bool Balanced(Node *root) {
if (root == NULL) { return true; }
if (Balanced(root->left) == false) { return false; }
if (Balanced(root->right) == false) { return false; }
int left = Height(root->left);
int right = Height(root->right);
int d = left - right;
if (d < -1 || d>1) { return false; }
return true;
}
用前序+中序还原二叉树
Node* buildTree(char preorder[], char inorder[], int size) {
if (size == 0) { return NULL; }
char rootValue = preorder[0];
int leftSize = find(inorder, size, rootValue);
assert(leftSize != -1);
Node *root = (Node*)malloc(sizeof(Node));
root->value = rootValue;
root->left = buildTree(preorder + 1, inorder, leftSize);
root->right = buildTree(preorder + 1 + leftSize, inorder + leftSize + 1, size - 1 - leftSize);
return root;
}
int find(char array[], int size, char v) {
for (int i = 0; i < size; i++) {
if (array[i] == v) { return i; }
}
return -1;
}
找二叉树的最近公共祖先
Node* lowestcommmonAncestor(Node *root, Node *p, Node *q) {
if (root == p || root == q) { return root; }
bool pInLeft = Find1(root->left, p);
bool qInLeft = Find1(root->left, q);
if (pInLeft&&qInLeft) { return lowestcommmonAncestor(root->left, p, q);}
if (pInLeft==false&&qInLeft==false) { return lowestcommmonAncestor(root->right, p, q); }
return root;
}
判断是否是完全二叉树
1.如果树不为空,层序遍历二叉树
2.如果遇到一个结点左右孩子都不为空,则pop该节点,将其左右孩子入队列
3.如果遇到一个结点,左孩子为空,右孩子不为空,则该树不一定完全二叉树
4.如果一个结点,左孩子不为空,右孩子为空;或者左右孩子都为空;则该节点之后的队列中的结点都为叶子结点,该树才为完全二叉树
bool isComplete(Node *root) {
if (root == NULL) { return true; }
std::queue<Node*> q;
q.push(root);
while (true) {
Node *front = q.front();
q.pop();
if (front == NULL) { break; }
q.push(front->left);
q.push(front->right);
}
//判断队列剩余元素是否全是NULL
while (!q.empty()) {
Node *front = q.front();
q.pop();
if (front != NULL) { return false; }
}
return true;
}
非递归前序遍历
void PreOrderNor(Node *root) {
std::stack<Node*> s;
Node *cur = root;
while (cur != NULL || s.empty()) {
while (cur != NULL) {
printf("%d", cur->value);
s.push();
cur = cur->left;
}
Node *top = s.top();
s.pop();
cur = top->right;
}
}