二叉树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 分钟前
C++入门基础篇(1)
开发语言·c++·学习
托尼沙滩裤19 分钟前
【js面试题】js的数据结构
前端·javascript·数据结构
逆水寻舟1 小时前
算法学习记录2
python·学习·算法
羞儿1 小时前
【读点论文】基于二维伽马函数的光照不均匀图像自适应校正算法
人工智能·算法·计算机视觉
续亮~2 小时前
6、Redis系统-数据结构-03-压缩列表
数据结构·数据库·redis
青衫酒1452 小时前
中国剩余定理
算法
鸽鸽程序猿2 小时前
【数据结构】顺序表
java·开发语言·数据结构·学习·算法·intellij idea
Thunter_3 小时前
[QT入门]树形视图控件
开发语言·c++·qt
Chris-zz3 小时前
C++:继承
开发语言·c++·算法·学习方法
硕风和炜3 小时前
【LeetCode:3033. 修改矩阵 + 模拟】
java·算法·leetcode·矩阵·模拟