6,675
社区成员
发帖
与我相关
我的任务
分享经历了对于二叉树的学习,相信大家对于二叉树一定有了深入的了解,接下来和J桑一起来刷起题来吧~


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);
}


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);
}


typedef struct TreeNode TreeNode;
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->right) && isSameTree(p->right, q->left);
}
bool isSymmetric(struct TreeNode* root)
{
if(root == NULL)
{
return true; // 空树是对称的
}
return isSameTree(root->left, root->right);
}

另一个树是否是这棵树的子树呢? 要解决这个问题,我们还需要用到两个树是否相同。

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);
}


并且它的输出是一个数组


为了返回这个数组,我们需要先获取树中结点个数。
前面我们实现过返回结点个数~
可以这样想, 一个结点的个数等于其左右子树结点个数之和加上它本身。

// ⼆叉树结点个数
// 一个结点的个数等于其左右子树结点个数之和加上它本身
int BinaryTreeSize(BTNode* root)
{
if (root == NULL)
{
return 0;
}
return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}
第二步: 动态的给数组申请内存
int* returnArr = (int* )malloc(sizeof(int) * (*returnSize));
第三步: 前序遍历树
前面也讲过~ 对于前序遍历来说,就是按照 根----左----右的顺序进行遍历。 如下图所示:

对于每个根节点来说,它的遍历方法都是 根----左----右,我们有时候可能认为遍历到一个结点的左孩子或者右孩子就要回去,其实是不对的。因为它的孩子的遍历方式也是 根----左----右。
typedef struct TreeNode TreeNode;
int BinaryTreeSize(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}
void PreOrder(TreeNode* root, int* arr, int* pi)
{
if (root == NULL)
{
return;
}
arr[(*pi)++] = root->val;
PreOrder(root->left, arr, pi);
PreOrder(root->right, arr, pi);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
//第一步求出树中结点个数
*returnSize = BinaryTreeSize(root);
//第二步动态的给数组申请内存
int* returnArr = (int* )malloc(sizeof(int) * (*returnSize));
//第三步前序遍历树
int i = 0;
PreOrder(root, returnArr, &i);
return returnArr;
}

左----右----根
typedef struct TreeNode TreeNode;
int BinaryTreeSize(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}
void PostOrder(TreeNode* root, int* arr, int* pi)
{
if (root == NULL)
{
return;
}
PostOrder(root->left, arr, pi);
PostOrder(root->right, arr, pi);
arr[(*pi)++] = root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
//第一步求出树中结点个数
*returnSize = BinaryTreeSize(root);
//第二步动态的给数组申请内存
int* returnArr = (int* )malloc(sizeof(int) * (*returnSize));
//第三步前序遍历树
int i = 0;
PostOrder(root, returnArr, &i);
return returnArr;
}

左----根----右
typedef struct TreeNode TreeNode;
int BinaryTreeSize(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}
void InOrder(TreeNode* root, int* arr, int* pi)
{
if (root == NULL)
{
return;
}
InOrder(root->left, arr, pi);
arr[(*pi)++] = root->val;
InOrder(root->right, arr, pi);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
//第一步求出树中结点个数
*returnSize = BinaryTreeSize(root);
//第二步动态的给数组申请内存
int* returnArr = (int* )malloc(sizeof(int) * (*returnSize));
//第三步前序遍历树
int i = 0;
InOrder(root, returnArr, &i);
return returnArr;
}

#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
int main()
{
// 1. 读取字符串
char arr[100];
scanf("%s",arr);
return 0;
}
获取了字符串后,我们要将每一个字符放入二叉树中,那么我们就要多次创立结点
BTNode* buyNode(char x)
{
BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
if (newnode == NULL)
{
perror("malloc fail!");
exit(1);
}
newnode->_data = x;
newnode->_left = newnode->_right = NULL;
return newnode;
}
拿到了结点之后我们要将结点放入二叉树中,这里用的是前序遍历的方法

BTNode* BinaryTreeCreate(BTDataType* a, int* pi)
{
if(a[(*pi)] == '#')
{
(*pi)++;
return NULL;
}
BTNode* root = buyNode(a[(*pi)++]);
root->_left = BinaryTreeCreate(a, pi);
root->_right = BinaryTreeCreate(a, pi);
return root;
}

总结如下:
#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode* _left;
struct BinaryTreeNode* _right;
}BTNode;
BTNode* buyNode(char x)
{
BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
if (newnode == NULL)
{
perror("malloc fail!");
exit(1);
}
newnode->_data = x;
newnode->_left = newnode->_right = NULL;
return newnode;
}
BTNode* BinaryTreeCreate(BTDataType* a, int* pi)
{
if(a[(*pi)] == '#')
{
(*pi)++;
return NULL;
}
BTNode* root = buyNode(a[(*pi)++]);
root->_left = BinaryTreeCreate(a, pi);
root->_right = BinaryTreeCreate(a, pi);
return root;
}
void InOrder(BTNode* root)
{
if(root == NULL)
{
return;
}
InOrder(root->_left);
printf("%c ",root->_data);
InOrder(root->_right);
}
int main()
{
// 1. 读取字符串
char arr[100];
scanf("%s",arr);
// 2. 根据字符串创立二叉树
//这里我们用前序遍历的方法创建
int i = 0;
BTNode* root = BinaryTreeCreate(arr,&i);
// 3. 最后我们中序输出
InOrder(root);
return 0;
}
⼆叉树性质:
对任何⼀棵⼆叉树, 如果度为 0 其叶结点个数为 n0 , 度为 2 的分⽀结点个数为 n2 ,则有: n0 = n2 + 1

证明上述性质: 假设⼀个⼆叉树有 a 个度为2的节点, b 个度为1的节点, c 个叶节点,则这个⼆叉树的边数是 2a+b。 另⼀⽅⾯,由于共有 a+b+c 个节点,所以边数等于 a+b+c-1 结合上⾯两个公式: 2a+b = a+b+c-1 , 即: a = c-1
根据二叉树的性质,叶子结点数 = 度为2的结点数 + 1。
完全二叉树有以下性质:
叶子结点数 = ⌈n/2⌉
完全二叉树的高度公式为:高度 = ⌊log2(N)⌋,其中N是结点数。
完全二叉树的性质:当有 N 个结点时,叶子结点数大约为 ⌊(N + 1) / 2⌋。
按前序遍历顺序,先访问根结点,再访问左子树,最后访问右子树。
先序遍历:EFHIGJK; 中序遍历:HFIEJKG。 从先序遍历中可以看出,E 是根结点。 答案:A
根据后序遍历可知,a 是根结点。
到这里,我们二叉树的知识就讲啦! J桑还是有点手忙脚乱的,各位观众老爷感觉怎么样呢?
谢谢大家!
文章来源: https://jiyufan-allforlove.blog.csdn.net/article/details/142503314
版权声明: 本文为博主原创文章,遵循CC 4.0 BY-SA 知识共享协议,转载请附上原文出处链接和本声明。