当前位置:网站首页>Leetcode brush questions - binary search tree related topics (98. Verify binary search tree, 235. The nearest common ancestor of binary search tree, 1038. From binary search tree to bigger sum tree, 5
Leetcode brush questions - binary search tree related topics (98. Verify binary search tree, 235. The nearest common ancestor of binary search tree, 1038. From binary search tree to bigger sum tree, 5
2022-08-04 11:12:00 【lonelyMangoo】
概念
二叉搜索树:
- 节点的左子树仅包含键 小于 节点键的节点.
- 节点的右子树仅包含键 大于 节点键的节点.
- 左右子树也必须是二叉搜索树.
The in-order traversal of a binary search tree is in ascending order.
98. 验证二叉搜索树
题目:98. 验证二叉搜索树
思路:Use in-order traversal to judge,Whether the current node is greater than the previous one.
//迭代写法
public boolean isValidBST(TreeNode root) {
Stack<TreeNode> stack = new Stack();
TreeNode cur = root;
TreeNode pre = null;
while (cur!=null || !stack.isEmpty()){
if(cur != null){
stack.add(cur);
cur = cur.left;
}
else {
cur = stack.pop();
if(pre!=null && pre.val>= cur.val){
return false;
}
//记录前一个节点
pre=cur;
cur=cur.right;
}
}
return true;
}
The level traversal I used at first,Because it ignores the situation that the whole is also greater than:
也就是例子中的5、3这种情况.
Of course, recursion can also be used here,It is also an in-order traversal and records the previous node
TreeNode pre=null;
public boolean isValidBST(TreeNode root) {
if(root == null ) return true;
boolean left = isValidBST(root.left);
//Inorder traversal operation part
if(pre!=null && root.val <= pre.val){
return false;
}
pre = root;
boolean right =isValidBST(root.right);
return left && right;
}
I think recursion is a bit tricky to understand,Although the idea is the same, you may not know which layer is the problem,Therefore, it is not easy to make mistakes in interviews or use iterations.
235. 二叉搜索树的最近公共祖先
题目:235. 二叉搜索树的最近公共祖先
This question only needs someone else to provide an idea to write it:
Both requested nodes are smaller than the current node,到左子树中去找
Both requested nodes are larger than the current node,到右子树中去找
If one is larger than the current node,一个比当前节点小,is the requested node.
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(p.val<root.val && q.val<root.val){
return lowestCommonAncestor(root.left, p, q);
}
else if(p.val>root.val && q.val>root.val){
return lowestCommonAncestor(root.right, p, q);
}
return root;
}
538. 把二叉搜索树转换为累加树
这两道题是一样的
538. 把二叉搜索树转换为累加树
1038. 从二叉搜索树到更大和树
重新建树
The first idea is easy to think of,First in-order traversal to get all the required values,Then inorder traversal again to assign values to the tree.
public TreeNode bstToGst(TreeNode root) {
List<Integer> list = new ArrayList<>();
inorder(root, list);
//将listThe value is set to what the title requires
for (int i = list.size() - 2; i >= 0; i--) {
list.set(i, list.get(i) + list.get(i + 1));
}
//重新赋值
build(root, list);
return root;
}
private static void build(TreeNode root, List<Integer> list) {
int i = 0;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
if (cur!=null){
cur.val = list.get(i++);
}
cur = cur.right;
}
}
}
private static void inorder(TreeNode root, List<Integer> list) {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
//Get the value of inorder traversal
list.add(cur.val);
cur = cur.right;
}
}
}
递归
Reverse inorder traversal is from big to small,用一个sumThe record can be accumulated.
int sum = 0;
public TreeNode bstToGst(TreeNode root) {
build(root);
return root;
}
//逆中序
private void build(TreeNode root){
if(root!=null){
build(root.right);
root.val+=sum;
sum=root.val;
build(root.left);
}
}
边栏推荐
- Graphic and text hands-on tutorial--ESP32 MQTT docking EMQX local server (VSCODE+ESP-IDF)
- 知其然,知其所以然,JS 对象创建与继承
- Camunda整体架构和相关概念
- win8和win10下,visual studio 2008 调试出现无响应的卡死问题解决
- MySQL 45 讲 | 10 MySQL为什么有时候会选错索引?
- 深度学习100例 —— 卷积神经网络(CNN)天气识别
- mae,mse,rmse分别利用sklearn和numpy实现
- mongo-导出数据到mysql
- 【LeetCode】701.二叉搜索树中的插入操作
- 技术干货 | 用零信任保护代码安全
猜你喜欢
学会使用set和map的基本接口
深度学习100例 —— 卷积神经网络(CNN)天气识别
入门MySql表的增删查改
什么是 DevOps?看这一篇就够了!
萌宠来袭,如何让“吸猫撸狗”更有保障?
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
Small program containers accelerate the construction of an integrated online government service platform
在 .NET MAUI 中如何更好地自定义控件
Maple 2022软件安装包下载及安装教程
职责链模式(responsibilitychain)
随机推荐
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三
Redis查询缓存
What is the terminal privilege management
zabbix部署
bitset的基本用法
【机器学习】:如何对你的数据进行分类?
第二批养老理财试点产品发行 一小时销售20亿元
数字知识库及考学一体化平台
Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
mongo-导出数据到mysql
Mysql——》类型转换符binary
复盘:经典的HR面试问题,这些问题可以挖掘你个人的素质,看看你是否合适合我们部门
win8和win10下,visual studio 2008 调试出现无响应的卡死问题解决
Zikko上市同时搭载HDMI2.1和2.5GbE新款雷电4扩展坞
[easyUI]修改datagrid表格中的值
ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法
ROI LTV CPA ECPM体系讲解
音频编辑 合唱
Maple 2022软件安装包下载及安装教程
【LeetCode】98.验证二叉搜索树