当前位置:网站首页>427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
2022-06-27 05:59:00 【liufeng2023】
617.合并二叉树

class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (root1 == nullptr) return root2;
if (root2 == nullptr) return root1;
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};

700.二叉搜索树中的搜索

class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (root == nullptr || root->val == val) return root;
if (root->val > val) return searchBST(root->left, val);
if (root->val < val) return searchBST(root->right, val);
return nullptr;
}
};

98. 验证二叉搜索树

class Solution {
private:
vector<int> res;
void traversal(TreeNode* root)
{
if (root == nullptr) return;
traversal(root->left);
res.push_back(root->val);
traversal(root->right);
}
public:
bool isValidBST(TreeNode* root) {
res.clear();
traversal(root);
for (int i = 1; i < res.size(); i++)
{
if (res[i - 1] < res[i])
{
continue;
}
else
{
return false;
}
}
return true;
}
};

530.二叉搜索树的最小绝对差

class Solution {
private:
vector<int> res;
void traversal(TreeNode* root)
{
if (root == nullptr) return;
traversal(root->left);
res.push_back(root->val);
traversal(root->right);
}
public:
int getMinimumDifference(TreeNode* root) {
res.clear();
traversal(root);
if (res.size() < 2) return 0;
int result = INT_MAX;
for (int i = 1; i < res.size(); i++)
{
result = std::min(result, res[i] - res[i - 1]);
}
return result;
}
};

边栏推荐
- 【QT小记】QT元对象系统简单认识
- IAR systems fully supports Centrino technology 9 series chips
- Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
- Contents in qlistwidget are not displayed
- Double position relay rxmd2-1mrk001984 dc220v
- Go log -uber open source library zap use
- 多线程基础部分Part2
- 我对于测试团队建设的意见
- 程序猿学习抖音短视频制作
- 思维的技术:如何破解工作生活中的两难冲突?
猜你喜欢

使用域名转发mqtt协议,避坑指南

Double position relay rxmd2-1mrk001984 dc220v

Go log -uber open source library zap use

双位置继电器JDP-1440/DC110V

Open the door small example to learn ten use case diagrams

leetcode299周赛记录

Kubesphere cluster configuration NFS storage solution - favorite

双位置继电器DLS-34A DC0.5A 220VDC

IAR systems fully supports Centrino technology 9 series chips

yaml文件加密
随机推荐
Spark 之 WholeStageCodegen
竣达技术丨多品牌精密空调集中监控方案
【QT小点】QT下载链接
mysql 查询时将状态改为相对应的文字
Create a basic WDM driver and use MFC to call the driver
Discussion on streaming media protocol (MPEG2-TS, RTSP, RTP, RTCP, SDP, RTMP, HLS, HDS, HSS, mpeg-dash)
Spark 之 built-in functions
Two position relay hjws-9440
Go log -uber open source library zap use
expect脚本中使用scp命令的方法,expect脚本中scp命令获取不了值的问题完美解决方法
【Cocos Creator 3.5.1】input. Use of on
Neon optimization 1: how to optimize software performance and reduce power consumption?
创建一个基础WDM驱动,并使用MFC调用驱动
30 SCM common problems and solutions!
数据库-索引
Assembly language - Wang Shuang Chapter 13 int instruction - Notes
Program ape learning Tiktok short video production
Wechat applet refreshes the current page
693. 交替位二进制数
Using domain name forwarding mqtt protocol, pit avoidance Guide