目录
1、单值二叉树
题目链接:
965. 单值二叉树 - 力扣(LeetCode)
https://leetcode.cn/problems/univalued-binary-tree/description/题目描述:
如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。
只有给定的树是单值二叉树时,才返回
true;否则返回false。
代码实现:
利用函数递归完成:先判断根节点是否为空,为空直接返回true,不为空继续判断其左右孩子节点的值与其是否相等,不相等就返回false,然后进行函数递归。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isUnivalTree(struct TreeNode* root) {
if(root==NULL)
{
return true;
}
//root非空,检查其左右子树的值是否等于根节点的值
if(root->left && root->left->val!=root->val)
{
return false;
}
if(root->right && root->right->val!=root->val)
{
return false;
}
return isUnivalTree(root->left) && isUnivalTree(root->right);
}
2、相同的树
题目链接:
100. 相同的树 - 力扣(LeetCode)
https://leetcode.cn/problems/same-tree/description/题目描述:
给你两棵二叉树的根节点
p和q,编写一个函数来检验这两棵树是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
代码实现:
仍然使用函数递归来解决:分步判断,先判断其结构,结构相同了再判断结点的值,结构和结点的值都相同了就是相同的树。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *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);
}
3、对称二叉树
题目链接:
101. 对称二叉树 - 力扣(LeetCode)
https://leetcode.cn/problems/symmetric-tree/description/题目描述:
给你一个二叉树的根节点
root, 检查它是否轴对称。如下图所示:

代码实现:
利用相同的树来解决:直接调用相同的树函数,一个树为root->left,另一个树为root->right,不过相同的树函数中递归部分要改成p->left和q->right,p->right和q->left。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *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->right) && isSameTree(p->right,q->left);
}
bool isSymmetric(struct TreeNode* root) {
//直接将根节点的左右孩子传给判断相同的树的函数
return isSameTree(root->left,root->right);
}
4、另一棵树的子树
题目链接:
572. 另一棵树的子树 - 力扣(LeetCode)
https://leetcode.cn/problems/subtree-of-another-tree/description/
题目描述:
给你两棵二叉树
root和subRoot。检验root中是否包含和subRoot具有相同结构和节点值的子树。如果存在,返回true;否则,返回false。二叉树
tree的一棵子树包括tree的某个节点和这个节点的所有后代节点。tree也可以看做它自身的一棵子树。
代码实现:
仍然使用相同的树:先判断根节点是否为空,为空返回false,不为空就调用相同的树函数比较root树和subroot树,如果没结束就分别递归判断左子树和右子树,最后返回两个递归的条件或。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *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);
}
5、二叉树的前序遍历
题目链接:
144. 二叉树的前序遍历 - 力扣(LeetCode)
https://leetcode.cn/problems/binary-tree-preorder-traversal/description/题目描述:
给你二叉树的根节点
root,返回它节点值的 前序 遍历。注意:返回的是数组形式哦!
代码实现:
先确定该二叉树中的节点个数,以确定malloc的数组的大小,然后再调用前序遍历函数,将遍历的每个值依次存入到数组中,最后返回该数组。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
//二叉树节点个数
int BinaryTreeSize(struct TreeNode* root)
{
if(root==NULL)
{
return 0;
}
return 1+BinaryTreeSize(root->left)+BinaryTreeSize(root->right);
}
//创建二叉树,前序遍历
void preOrder(struct 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
*returnSize = BinaryTreeSize(root);
//创建大小为returnSize数组
int* arr=(int*)malloc(sizeof(int)*(*returnSize));
//使用创建二叉树函数将节点存入数组中
int i=0;
preOrder(root,arr,&i);
return arr;
}
6、二叉树的中序遍历
题目链接:
94. 二叉树的中序遍历 - 力扣(LeetCode)
https://leetcode.cn/problems/binary-tree-inorder-traversal/description/题目描述:
给定一个二叉树的根节点
root,返回 它的 中序 遍历 。返回的依旧是数组形式。
代码实现:
和前序遍历的思路一样,只是遍历方式不同。
先确定该二叉树中的节点个数,以确定malloc的数组的大小,然后再调用中序遍历函数,将遍历的每个值依次存入到数组中,最后返回该数组。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
//求二叉树节点个数
int binarytreeSize(struct TreeNode* root)
{
if(root==NULL)
{
return 0;
}
return 1+binarytreeSize(root->left)+binarytreeSize(root->right);
}
//中序遍历存入数组
void Inorder(struct 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
*returnSize=binarytreeSize(root);
//创建数组
int* arr=(int*)malloc(sizeof(int)*(*returnSize));
//中序遍历存入数组
int i=0;
Inorder(root,arr,&i);
return arr;
}
7、二叉树的后序遍历
题目描述:
给你一棵二叉树的根节点
root,返回其节点值的 后序遍历 。返回的也是数组形式的遍历结果。
代码实现:
和前序遍历的思路一样,只是遍历方式不同。
先确定该二叉树中的节点个数,以确定malloc的数组的大小,然后再调用后序遍历函数,将遍历的每个值依次存入到数组中,最后返回该数组。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int binarytreeSize(struct TreeNode* root)
{
if(root==NULL)
{
return 0;
}
return 1+binarytreeSize(root->left)+binarytreeSize(root->right);
}
//后序遍历
void postOrder(struct 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的值
*returnSize=binarytreeSize(root);
//创建数组
int* arr=(int*)malloc(sizeof(int)*(*returnSize));
//利用后序遍历存储到数组中
int i=0;
postOrder(root,arr,&i);
return arr;
}
8、二叉树的遍历
题目链接:
二叉树遍历_牛客题霸_牛客网
https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef题目描述:
描述:
编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。 例如如下的先序遍历字符串: ABC##DE#G##F### 其中“#”表示的是空格,空格字符代表空树。建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。
输入描述:
输入包括1行字符串,长度不超过100。
输出描述:
可能有多组测试数据,对于每组数据, 输出将输入字符串建立二叉树后中序遍历的序列,每个字符后面都有一个空格。 每个输出结果占一行。
代码实现:
先用字符数组存储表示该二叉树的字符串,然后为创建的二叉树创建结构和节点,再根据前序遍历的结果创建出二叉树(这里用递归来创建二叉树),遇到#直接返回NULL,建立完二叉树后,对该二叉树进行中序遍历,输出中序遍历结果。
#include <stdio.h>
#include <stdlib.h>
//创建二叉树结构
typedef struct BinaryTreeNode {
char data;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BTNode;
//创建节点
BTNode* buynode(char x)
{
BTNode* newnode=(BTNode*)malloc(sizeof(BTNode));
newnode->data=x;
newnode->left=newnode->right=NULL;
return newnode;
}
//创建二叉树
BTNode* CreateTree(char* arr,int* pi)
{
if(arr[(*pi)]=='#')
{
(*pi)++;//进循环了才需要++
return NULL;
}
BTNode* root = buynode(arr[(*pi)++]);
root->left=CreateTree(arr,pi);
root->right=CreateTree(arr,pi);
return root;
}
//中序遍历
void InOrder(BTNode* root)
{
if(root==NULL)
{
return;
}
InOrder(root->left);
printf("%c ",root->data);
InOrder(root->right);
}
int main() {
//读取字符串到数组中
char arr[101]={0};
scanf("%s",arr);
//根据先序遍历创建二叉树
int i=0;
BTNode* root=CreateTree(arr, &i);
//输出中序遍历结果
InOrder(root);
return 0;
}
306





