当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢
Win11怎么重装显卡驱动程序?Win11显卡驱动怎么卸载重装?
cubemx stm32 afm3000 module gas flow sensor driver code
面试蚂蚁(P7)竟被MySQL难倒,奋发图强后二次面试入职蚂蚁金服
Heap Sort
Mysql高级篇学习总结13:多表连接查询语句优化方法(带join语句)
Maple 2022 software installation package download and installation tutorial
图文手把手教程--ESP32 OTA空中升级(VSCODE+IDF)
【黄啊码】MySQL入门—1、SQL 的执行流程
Maple 2022软件安装包下载及安装教程
A topic of map
随机推荐
Zikko上市同时搭载HDMI2.1和2.5GbE新款雷电4扩展坞
What is the terminal privilege management
What is the principle of thermal imaging temperature measurement?Do you know?
C#/VB.NET:在 Word 中设置文本对齐方式
Oracle中对临时表空间执行shrink操作
ORA-00054 资源正忙
Mysql——》类型转换符binary
JUC(1)线程和进程、并发和并行、线程的状态、lock锁、生产者和消费者问题
8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
强烈推荐一款优秀且通用的后台管理系统
RL78 development environment
【虹科案例】基于3D相机组装家具
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
萌宠来袭,如何让“吸猫撸狗”更有保障?
【LeetCode】98.验证二叉搜索树
热成像测温的原理是什么呢?你知道吗?
关于架构的思考
【Idea series】idea configuration
Why are all hotel bathrooms transparent?
Using .NET to simply implement a high-performance clone of Redis (2)