目录
1、另一棵树的子树
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
//两个都为空
if(p == NULL && q == NULL)
return true;
//一个为空一个不为空
if(p == NULL || q == NULL)
return false;
//都不为空
if(p->val != q->val)
return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
//找到为空
if(root == NULL)
return false;
//找
if(isSameTree(root, subRoot))
return true;
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
2、对称二叉树
bool _isSymmetric(struct TreeNode* left, struct TreeNode* right)
{
//两者为空
if(left == NULL && right == NULL)
return true;
//走到这,两者不同时为空
if(left == NULL || right == NULL)
return false;
//走到这,两者都不为空
if(left->val != right->val)
return false;
return _isSymmetric(left->left, right->right)
&& _isSymmetric(left->right, right->left);
}
bool isSymmetric(struct TreeNode* root){
if(root == NULL)
return true;
return _isSymmetric(root->left, root->right);
}
3、相同的树
bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(q == NULL && p == NULL)
return true;
//走到这两个必不同时为空
if(q == NULL || p == NULL)
return false;
//走到这两个必不为空
if(p->val != q->val)
return false;
return isSameTree(p->left, q->left)
&&isSameTree(p->right, q->right);
}
4、单值二叉树
法1
//法1
bool isUnivalTree(struct TreeNode* root){
if(root == NULL)
{
return true;
}
if(root->left && root->val != root->left->val)
{
return false;
}
if(root->right && root->val != root->right->val)
{
return false;
}
return isUnivalTree(root->left)
&& isUnivalTree(root->right);
}
法2
//法2
bool _isUnivalTree(struct TreeNode* root, int val)
{
if(root == NULL)
{
return true;
}
if(root->val != val)
return false;
return _isUnivalTree(root->left, val)
&& _isUnivalTree(root->right, val);
}
bool isUnivalTree(struct TreeNode* root){
return _isUnivalTree(root, root->val);
}
5、平衡二叉树
//最大深度 = 左右子树最大深度 + 1
int MaxDepth(struct TreeNode* root)
{
if(root == NULL)
return 0;
int left = MaxDepth(root->left);
int right = MaxDepth(root->right);
return left > right ? left + 1 : right + 1;
}
bool isBalanced(struct TreeNode* root){
if(root == NULL)
return true;
if(fabs(MaxDepth(root->left) - MaxDepth(root->right)) > 1)
return false;
return isBalanced(root->left) && isBalanced(root->right);
}
6、翻转二叉树
struct TreeNode* invertTree(struct TreeNode* root) {
if(root==NULL)
return NULL;
struct TreeNode* temp=invertTree(root->left);
root->left=invertTree(root->right);
root->right=temp;
return root;
}
7、二叉树的前序遍历
int TreeSize(struct TreeNode* root)
{
return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
}
void prevorder(struct TreeNode* root, int* a, int* pi)
{
if(root == NULL)
return;
a[*pi] = root->val;
(*pi)++;
prevorder(root->left, a, pi);
prevorder(root->right, a, pi);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int n = TreeSize(root);
*returnSize = n;
int* a = (int*)malloc(sizeof(int) * n);
int i = 0;
prevorder(root, a, &i);
return a;
}
8、二叉树的中序遍历
//计算树的节点个数
int SizeofTree(struct TreeNode* root)
{
return root == NULL? 0 : SizeofTree(root->left) + SizeofTree(root->right) +1;
}
void InOrder(struct TreeNode* root, int* a, int* pi)
{
if(root == NULL)
return;
InOrder(root->left, a, pi);
a[*pi] = root->val;
(*pi)++;
InOrder(root->right, a, pi);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int len = SizeofTree(root);
*returnSize = len;
int* a = (int*)malloc(sizeof(int) *len);
int i = 0;
InOrder(root, a, &i);
return a;
}
9、二叉树的后序遍历
int SizeofTree(struct TreeNode* root)//计算树的节点个数
{
if (root == NULL)
return 0;
return SizeofTree(root->left) + SizeofTree(root->right) + 1;
}
void PostOrder(struct TreeNode* root, int* a, int* pi)
{
if (root == NULL)
return;
PostOrder(root->left, a, pi);
PostOrder(root->right, a, pi);
a[*pi] = root->val;
(*pi)++;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize) {
int len = SizeofTree(root);
*returnSize = len;
int* a = (int*)malloc(sizeof(int) * len);
int i = 0;
PostOrder(root, a, &i);
return a;
}
10、二叉树遍历
#include <stdio.h>
typedef struct BinaryTree
{
struct BinaryTree* left;
struct BinaryTree* right;
char val;
}Tree;
void PrintPrevOrder(Tree* root)
{
if(root == NULL)
return;
PrintPrevOrder(root->left);
printf("%c ", root->val);
PrintPrevOrder(root->right);
}
Tree* PrevOrderTree(char* a, int* pi)
{
if(a[*pi] == '#')
{
(*pi)++;
return NULL;
}
Tree* root = (Tree*)malloc(sizeof(Tree));
if(root == NULL)
{
perror("malloc");
exit(-1);
}
root->val = a[*pi];
(*pi)++;
//返回时才链接上
root->left = PrevOrderTree(a, pi);
root->right = PrevOrderTree(a, pi);
return root;
}
int main()
{
char a[101] = {0};
scanf("%s", a);
int len = strlen(a);
int i = 0;
Tree* root = PrevOrderTree(a, &i);
PrintPrevOrder(root);
}
11、二叉树的所有路径
//计算叶子节点个数
int leafnodesize(struct TreeNode* root)
{
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
return leafnodesize(root->left) + leafnodesize(root->right);
}
void _binaryTreePaths(struct TreeNode* root, char** ret, char* curPath, int* returnSize, int cur)
{
if(root == NULL)
return ;
//不是叶子节点
if(root->left != NULL || root->right != NULL)
{
curPath[cur++] = root->val;
_binaryTreePaths(root->left, ret, curPath,returnSize, cur);
_binaryTreePaths(root->right, ret, curPath,returnSize, cur);
}
else//遇到叶子节点
{
char* tmp = (char*)malloc(sizeof(char) * 300);
int j = 0;
for (int i = 0; i < cur; i++)
{
j += sprintf(tmp + j, "%d->", curPath[i]);
}
sprintf(tmp + j, "%d", root->val);
ret[(*returnSize)++] = tmp;
}
}
char ** binaryTreePaths(struct TreeNode* root, int* returnSize){
*returnSize = 0;
//计算叶子节点个数
int n = leafnodesize(root);
char** ret = (char**)malloc(sizeof(char*)* n);
char* curPath = (char*)malloc(sizeof(char) * 100);
int cur = 0;
_binaryTreePaths(root, ret, curPath,returnSize, cur);
return ret;
}