当前位置:网站首页>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;
}
};

边栏推荐
- LeetCode-515. Find the maximum value in each tree row
- Assembly language - Wang Shuang Chapter 13 int instruction - Notes
- 【QT小点】实现看门狗功能,检测外部程序是否在运行
- [FPGA] design and implementation of frequency division and doubling based on FPGA
- 使用 WordPress快速个人建站指南
- Software testing year end summary report template
- What's new in redis4.0 - active memory defragmentation
- Get system volume across platforms in unity
- Contents in qlistwidget are not displayed
- Netease cloud music params and encseckey parameter generation code
猜你喜欢

JS to implement bidirectional data binding

思维的技术:如何破解工作生活中的两难冲突?
![Navigation [machine learning]](/img/79/8311a409113331e72f650a83351b46.png)
Navigation [machine learning]

双位置继电器XJLS-8G/220

Discussion on streaming media protocol (MPEG2-TS, RTSP, RTP, RTCP, SDP, RTMP, HLS, HDS, HSS, mpeg-dash)

免费的 SSH 和 Telnet 客户端PuTTY

Win 10 如何打开环境变量窗口

Assembly language - Wang Shuang Chapter 13 int instruction - Notes

块级元素&行内元素

Basic concepts of neo4j graph database
随机推荐
openresty使用文档
QListWidget中的内容不显示
QT using Valgrind to analyze memory leaks
双位置继电器HJWS-9440
Database - index
Comprehensive application of OpenCV in contour detection and threshold processing
Dual position relay dls-34a dc0.5a 220VDC
IP网络通信的单播、组播和广播
函数栈帧的形成与释放
Opencv implements object tracking
函数式 连续式
《汇编语言-王爽》第3章笔记及实验
1317. 将整数转换为两个无零整数的和
Double position relay rxmd2-1mrk001984 dc220v
Spark 之 WholeStageCodegen
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
JVM调优思路
Junda technology - centralized monitoring scheme for multi brand precision air conditioners
310. minimum height tree
Two position relay rxmvb2 r251 204 110dc