二叉树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直到遍历完所有节点。

相关推荐
曦月逸霜19 分钟前
第34次CCF-CSP认证真题解析(目标300分做法)
数据结构·c++·算法
galaxy_strive22 分钟前
绘制饼图详细过程
开发语言·c++·qt
海的诗篇_1 小时前
移除元素-JavaScript【算法学习day.04】
javascript·学习·算法
自动驾驶小卡2 小时前
A*算法实现原理以及实现步骤(C++)
算法
Unpredictable2222 小时前
【VINS-Mono算法深度解析:边缘化策略、初始化与关键技术】
c++·笔记·算法·ubuntu·计算机视觉
编程绿豆侠2 小时前
力扣HOT100之多维动态规划:1143. 最长公共子序列
算法·leetcode·动态规划
珂朵莉MM2 小时前
2021 RoboCom 世界机器人开发者大赛-高职组(初赛)解题报告 | 珂学家
java·开发语言·人工智能·算法·职场和发展·机器人
PingdiGuo_guo2 小时前
C++智能指针的知识!
开发语言·c++
Chuncheng's blog2 小时前
CentOS 7如何编译安装升级gcc至7.5版本?
linux·运维·c++·centos