当前位置:网站首页>427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)
427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)
2022-06-27 06:01:00 【liufeng2023】
617. Merge binary tree

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. Search in binary search tree

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. Verify binary search tree

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. The minimum absolute difference of binary search tree

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;
}
};

边栏推荐
- Asp.Net Core6 WebSocket 简单案例
- Using domain name forwarding mqtt protocol, pit avoidance Guide
- Qt使用Valgrind分析内存泄漏
- [FPGA] design and implementation of frequency division and doubling based on FPGA
- Change the status to the corresponding text during MySQL query
- js实现双向数据绑定
- 爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)
- LeetCode-515. 在每个树行中找最大值
- 免费的 SSH 和 Telnet 客户端PuTTY
- Leetcode298 weekly race record
猜你喜欢
![Navigation [machine learning]](/img/79/8311a409113331e72f650a83351b46.png)
Navigation [machine learning]

Open the door small example to learn ten use case diagrams
![[FPGA] design and implementation of frequency division and doubling based on FPGA](/img/84/75d473d3d8e670260ba16d06705c2f.png)
[FPGA] design and implementation of frequency division and doubling based on FPGA

How win 10 opens the environment variables window

双位置继电器HJWS-9440

汇编语言-王爽 第9章 转移指令的原理-笔记

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

Program ape learning Tiktok short video production

LeetCode-515. 在每个树行中找最大值

30 SCM common problems and solutions!
随机推荐
Webrtc Series - Network Transport 7 - ice Supplement nominations and ice Modèle
OpenCV的轮廓检测和阈值处理综合运用
openresty使用文档
How to check the frequency of memory and the number of memory slots in CPU-Z?
项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
程序猿学习抖音短视频制作
[cocos creator 3.5.1] addition of coordinates
免费的 SSH 和 Telnet 客户端PuTTY
My opinion on test team construction
JVM的垃圾回收机制
Configuring the help class iconfiguration in C # NETCORE
[FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design
Assembly language - Wang Shuang Chapter 3 notes and experiments
Functional continuous
[collection] Introduction to basic knowledge of point cloud and functions of point cloud catalyst software
导航【机器学习】
Wechat applet websocket use case
Multithreading basic part2
C语言练手小项目(巩固加深知识点理解)
【QT小作】使用结构体数据生成读写配置文件代码