二叉树OJ题

目录

[1. 单值二叉树](#1. 单值二叉树)

[2. 二叉树的最大深度](#2. 二叉树的最大深度)

[3. 二叉树的前序便利](#3. 二叉树的前序便利)

[4. 相同的树](#4. 相同的树)

[5. 对称二叉树](#5. 对称二叉树)

[6 . 另一棵树的子树](#6 . 另一棵树的子树)

[7. KY11 二叉树遍历](#7. KY11 二叉树遍历)

[8 . 翻转二叉树](#8 . 翻转二叉树)

[9. 判断平衡二叉树](#9. 判断平衡二叉树)



1. 单值二叉树

oj链接

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#if 0
如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。

只有给定的树是单值二叉树时,才返回 true;否则返回 false。
#endif
struct TreeNode {
	int val;
	struct TreeNode* left;
	struct TreeNode* right;
	
};
//bool _isUnivalTree(struct TreeNode* root, int x)
//{
//	//一直深度便利到为空节点如果还一样就返回true
//	if (root == NULL)
//	{
//		return true;
//	}
//	if (root->val != x)
//	{
//		return false;
//	}
//	bool res1 = _isUnivalTree(root->left, x);
//	if (res1 == false)
//	{
//		return false;
//	}
//	bool res2 = _isUnivalTree(root->right, x);
//	if (res2 == false)
//	{
//		return false;
//	}
//	else
//	{
//		return true;
//	}
//}
思路就是前序便利二叉树如果遇见不相等的直接返回false
//bool isUnivalTree(struct TreeNode* root) {
//	return _isUnivalTree(root, root->val);
//}
// 
//第二种
//思路就是每次递归的是根节点让其与左右节点进行比较
bool isUnivalTree(struct TreeNode* root) {
    if (root == NULL)
    {
        return true;
    }
    //防止root->left为空对空指针进行解引用
    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. 二叉树的最大深度

oj链接

cpp 复制代码
//思路就是要左右子树比较高的那个高度加1
    int maxDepth(struct TreeNode* root) {
        if (root == NULL)
        {
            return 0;
        }
        int l = maxDepth(root->left);
        int r = maxDepth(root->right);
        int max = l > r ? l : r;
        return max + 1;
}

3. 二叉树的前序便利

二叉树的前序便利

cpp 复制代码
typedef struct TreeNode TreeNode;
 int count1(TreeNode* node)
 {
     return node == NULL ? 0 : count1(node->left) + count1(node->right) + 1;
 }
 void _preorderTraversal(int* a, TreeNode* root, int* i)
 {
     if (root == NULL)
     {
         return;
     }
     a[(*i)++] = root->val;
     _preorderTraversal(a, root->left, i);
     _preorderTraversal(a, root->right, i);
 }
int* preorderTraversal(struct TreeNode* root, int* returnSize) 
{
    int count = count1(root);
    int* ret = (int*)malloc(sizeof(int) * count);
    *returnSize = count;
    int i = 0;
    _preorderTraversal(ret, root, &i);
    return ret;
}

思路就是开动态开辟一个数组具体个数根据数的节点个数来定,注意这里在递归时要传递数组下标的地址。

4. 相同的树

相同的树

cpp 复制代码
typedef struct TreeNode TreeNode;
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    //如果p树为空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);
}

5. 对称二叉树

对称二叉树

cpp 复制代码
 typedef struct TreeNode TreeNode;
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    //如果p树为空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);
}

对称二叉树就是比较一棵树的左子树与其右子树是否一样

6 . 另一棵树的子树

另一棵树的子树

cpp 复制代码
  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 (q->val != p->val)
      {
          return false;
      }
      return isSameTree(q->left, p->left) && isSameTree(q->right, p->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);

}

这里的思路就是把树的每一个节点当做新树的根节点取与另一棵树作比较,找到相同的就停止。

7. KY11 二叉树遍历

二叉树便利

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct BinaryTreeNode
{
	char data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;

BTNode* TreeCreate(char* arr,int *i)
{
	if (arr[*i] == '#')
	{
		(*i)++;
		return NULL;
	}
	BTNode* root = (BTNode*)malloc(sizeof(BTNode));
	root->data = arr[(*i)++];
	root->left = TreeCreate(arr, i);
	root->right = TreeCreate(arr, i);
	return root;
	
}
void  TreeInorede(BTNode* tree)
{
	if (tree == NULL)
	{
		//printf(" ");
		return;
	}
	TreeInorede(tree->left);
	printf("%c ", tree->data);
	TreeInorede(tree->right);
}
int main()
{
	char arr[100] = {0};
	scanf("%s", arr);
	int i = 0;
	BTNode *tree = TreeCreate(arr,&i);
	TreeInorede(tree);
	return 0;
}

二叉树便利我们根据注意应该传入数组的下标的地址。

8 . 翻转二叉树

翻转二叉树

cpp 复制代码
struct TreeNode* invertTree(struct TreeNode* root) {
    if(root==NULL)
    {
        return NULL;
    }
    struct TreeNode * tem = root->left;
    root->left = root->right;
    root->right = tem;
    invertTree(root->left);
    invertTree(root->right);
    return root;
}

9. 判断平衡二叉树

平衡二叉树

平衡二叉树是每个节点的左右字数的高度不超过1

cpp 复制代码
int depth (struct TreeNode *root)
 {
    if(root==NULL)
    {
        return 0;
    }
     int  left = depth(root->left);
     int right = depth(root->right);
     return left>right?left+1:right+1;
 }
bool isBalanced(struct TreeNode* root) {
    if(root==NULL)
    {
        return true;
    }
    int a = depth(root->left);
    int b = depth(root->right);
    int ret = a>b?a-b:b-a;
    if(ret>1)
    {
        return false;
    }
    return isBalanced(root->left) && isBalanced(root->right);
    
}

思路就是看看每个节点的左右子树的高度差,只要有一个节点高度差大于1就返回false直到遍历完所有节点。

相关推荐
超级码力6663 小时前
【Latex文件架构】Latex文件架构模板
算法·数学建模·信息可视化
穿条秋裤到处跑3 小时前
每日一道leetcode(2026.04.29):二维网格图中探测环
算法·leetcode·职场和发展
Merlos_wind3 小时前
HashMap详解
算法·哈希算法·散列表
汉克老师4 小时前
GESP2025年3月认证C++五级( 第三部分编程题(1、平均分配))
c++·算法·贪心算法·排序·gesp5级·gesp五级
Yzzz-F6 小时前
Problem - 2205D - Codeforces
算法
智者知已应修善业7 小时前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
Halo_tjn7 小时前
Java Set集合相关知识点
java·开发语言·算法
生成论实验室8 小时前
《事件关系阴阳博弈动力学:识势应势之道》第四篇:降U动力学——认知确定度的自驱演化
人工智能·科技·神经网络·算法·架构
AI科技星8 小时前
全域数学·72分册:场计算机卷【乖乖数学】
算法·机器学习·数学建模·数据挖掘·量子计算
云泽8088 小时前
C++11 核心特性全解:列表初始化、右值引用与移动语义实战
开发语言·c++