当前位置:网站首页>二叉树常见面试题
二叉树常见面试题
2022-06-26 09:37:00 【努力变好的zz】
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using std::cout;
using std::cin;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {
}
};
//题目1.后继者
//设计一个算法,找出二叉搜索树中指定节点的“下一个”节点(也即中序后继)。
class Solution1 {
public:
void Tree(TreeNode* root, TreeNode** ret, int* n)
{
if (root == nullptr)
{
return;
}
Tree(root->left, ret, n);
ret[(*n)++] = root;
Tree(root->right, ret, n);
}
int TreeSize(TreeNode* root)
{
if (root == nullptr)
{
return 0;
}
int leftSize = TreeSize(root->left);
int rightSize = TreeSize(root->right);
int sum = leftSize + rightSize + 1;
return sum;
}
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
int Size = TreeSize(root);//计算二叉树大小
TreeNode** ret = new TreeNode * [Size];
int n = 0;
Tree(root, ret, &n);
TreeNode* next = NULL;
for (int i = 0; i < n; i++)
{
if (ret[i] == p && i < n - 1)
{
next = ret[i + 1];
}
}
return next;
}
};
//树形DP套路口诀
//(1)假设以X节点为头,假设可以向X左树和X右树要任何信息
//(2)在上一步的假设下,讨论以X为头节点的树,得到答案的可能性(最重要)
//(3)列出所有可能性后,确定到底需要向左树和右树要什么样的信息
//(4)把左树信息和右树信息求全集,就是任何一棵子树都需要返回的信息S
//(5)递归函数都返回S,每一棵子树都这么要求
//题目2.平衡二叉树
//给定一个二叉树,判断它是否是高度平衡的二叉树。
//本题中,一棵高度平衡二叉树定义为:
//一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
int maxDepth(struct TreeNode* root) {
if (root == NULL)
{
return 0;
}
int treeleft = maxDepth(root->left);
int treeright = maxDepth(root->right);
return treeleft > treeright ? treeleft + 1 : treeright + 1;
}
bool isBalanced(struct TreeNode* root) {
if (root == NULL)
{
return true;
}
int treeleft = maxDepth(root->left);//判断左树是不是平衡二叉树
int treeright = maxDepth(root->right);//判断右树是不是二叉树
return abs(treeleft - treeright) < 2 && isBalanced(root->left) && isBalanced(root->right);
}
//题目3.树中节点间最远距离
//给定一棵二叉树的头节点head,任何两个节点之间都存在距离,给定一棵二叉树的头节点head,任何两个节点之间都存在距离
//返回整棵二叉树最大距离
struct Info3
{
int MaxDistance;//记录节点中最大距离
int height;
Info3(int a, int b)
{
MaxDistance = a;
height = b;
}
};
class Solution3 {
public:
Info3* Process3(TreeNode* head)
{
if (head == nullptr)
{
return new Info3(0, 0);
}
Info3* left = Process3(head->left);
Info3* right = Process3(head->right);
int height = fmax(left->MaxDistance, right->MaxDistance) + 1;
int Distance = fmax(left->height + right->height + 1, fmax(left->MaxDistance, right->MaxDistance));
delete left;
delete right;
return new Info3(Distance, height);
}
int MaxDistance(TreeNode* head)//主函数
{
return Process3(head)->MaxDistance;
}
};
//题目4.最大二叉搜索
//给定一棵二叉树的头节点head,
//返回这颗二叉树中最大的二叉搜索子树的节点数量
struct Info4
{
bool isALLBST;//是否是二叉搜素树
int max;//记录最大节点
int min;//记录最小节点
int Sum;//最大二叉搜索树的节点个数
Info4(bool s, int _max, int _min, int _Sum)
{
isALLBST = s;
max = _max;
min = _min;
Sum = _Sum;
}
};
class Solution4
{
public:
Info4* Process4(TreeNode* head)
{
if (head == nullptr)
{
return nullptr;
}
Info4* left = Process4(head->left);
Info4* right = Process4(head->right);
int max = head->val;
int min = head->val;
if (left != nullptr)
{
max = fmax(max, left->max);
min = fmin(min, right->min);
}
if (right != nullptr)
{
max = fmax(max, right->max);
min = fmin(min, right->min);
}
bool s = false;//默认不是二叉搜素树
int sum = 0;
if (left != nullptr)
{
sum = left->Sum;
}
if (right != nullptr)
{
sum = right->Sum;
}
if (left == nullptr ? true : (left->isALLBST)
&& right == nullptr ? true : (right->isALLBST)
&& left == nullptr ? true : (left->max<head->val)
&& right == nullptr ? true : (right->min>head->val)
)
{
s = true;
sum = (left == nullptr ? 0 : left->Sum) + (right == nullptr ? 0 : right->Sum) + 1;
}
delete left;
delete right;
return new Info4(s, max, min, sum);
}
int MaxBstSize(TreeNode* head)
{
return Process4(head)->Sum;
}
};
//题目5.判断满二叉树
//给定一个数的头结点,返回这棵树是不是满二叉树
//技巧:满二叉树符合2^高度-1 = 节点个数
struct Info5
{
int height;//记录树的高度
int Sum;//记录节点的个数
Info5(int _height, int _Sum)
{
height = _height;
Sum = _Sum;
}
};
class Solution5
{
public:
Info5* Process5(TreeNode* head)
{
if (head == nullptr)
{
return new Info5(0, 0);
}
Info5* left = Process5(head->left);
Info5* right = Process5(head->right);
int height = fmax(left->height, right->height) + 1;
int Sum = left->Sum + right->Sum + 1;
delete left;
delete right;
return new Info5(height, Sum);
}
bool IsAllTree(TreeNode* head)
{
Info5* s = Process5(head);
bool f = pow(2, s->height) == s->Sum;
delete s;
return f;
}
};
边栏推荐
- Get the clicked position in the recyclerview
- cmake / set 命令
- libmagic 介绍
- 3行3列整形二维数组,求对角之和
- jar版本冲突问题解决
- Problems encountered in the application and development of Hongmeng and some roast
- Small example of SSM project, detailed tutorial of SSM integration
- Software testing - how to select the appropriate orthogonal table
- MySQL 13th job - transaction management
- Blog article index summary -- Software Engineering
猜你喜欢
MySQL 13th job - transaction management
C中字符串基本操作
创建对象的时候堆内存的分配
Druid data source for background monitoring
MySQL 11th job - view application
Detailed explanation of winsorflow quantum installation process
Develop current learning objectives and methods
Extracting public fragments from thymeleaf
MySQL job 11 - application de la vue
MySQL第十三次作业-事务管理
随机推荐
MySQL项目7总结
微软 Edge 浏览器 IE 模式标签页出现卡死情况,已通过回滚更新修复
MySQL project 7 Summary
Retrofit common request methods and comments, post, get heard file upload
What are the symbolic and direct references of the JVM
MySQL项目8总结
Selection of webrtc video codec type VP8 H264 or other? (openh264 encoding, ffmpeg decoding)
Under the double reduction, the amount of online education has plummeted. Share 12 interesting uses of webrtc
Win10安装tensorflow-quantum过程详解
Pytest configuration file
How do technicians send notifications?
DBSCAN
[sans titre]
Extracting public fragments from thymeleaf
【二分查找】4. 寻找两个正序数组的中位数
The first batch of 12 enterprises settled in! Opening of the first time-honored product counter in Guangzhou
The basis of C language grammar -- function definition learning
Various errors encountered by tensorflow
How to change the QR code material color of wechat applet
MySQL job 11 - application de la vue