Second largest node in the BST
Find the second largest node in the BST
分析:
如果root有右节点,很明显第二大的node有可能在右子树里。唯一不满足的条件就是右子树只有一个node. 这个时候root就是第二大node. 所以我们需要把root(可能是第二大node)也传下去。
如果root没有右节点,我们只要找左子树的最大node就可以了。
public class Solution {
public Node secondLargest(Node root) {
return helper(root, null);
} private Node helper(Node root, Node previous) {
if (root.right != null) {
return helper(root.right, root);
} if (root.left != null) {
return rightMost(root.left);
} return previous;
} private Node rightMost(Node node) {
while (node.right != null) {
node = node.right;
}
return node;
}
」
Second largest node in the BST的更多相关文章
- [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- Leetcode: Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- [Leetcode]450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- [Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- 450. Delete Node in a BST 删除bst中的一个节点
[抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. ...
- LeetCode OJ 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- [LeetCode] 450. Delete Node in a BST 删除二叉搜索树中的节点
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- LC 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
随机推荐
- js+下载文件夹
一.此方法火狐有些版本是不支持的 window.location.href = 'https://*****.oss-cn-**.aliyuncs.com/*********'; 二.为了解决火狐有些 ...
- vundle
vundle插件的使用方法: http://adam8157.info/blog/2011/12/use-vundle-to-manage-vim-plugins http://adam8157.in ...
- 阿里云修改主机名hostname
一.永久修改主机名的方法(针对于普通的服务器) 1.通过hostname命令修改. [root@izwz9f7pm0tw36neb1j7gmz ~]# hostname node1 修改完之后发现主机 ...
- mybatis集成spring
1.为什么mybatis要去集成spring?===>简单来说就是为了让spring 来帮我们管理事务!!如果没有spring去帮助管理事务,那么需要我们自己去手动去维护事务,例如:发生异常需要 ...
- spring MVC 拦截有几种实现方式
spring MVC 拦截有几种实现方式 实现HandelInterceptor接口方式 继承HandelInterceptor 的方式.一般有这两种方式 spring 如何走单元测式 ...
- 梯度提升树GBDT总结
提升树的学习优化过程中,损失函数平方损失和指数损失时候,每一步优化相对简单,但对于一般损失函数优化的问题,Freidman提出了Gradient Boosting算法,其利用了损失函数的负梯度在当前模 ...
- CCF 2017 09-02 公共钥匙盒
CCF 2017 09-02 公共钥匙盒 1.用快速排序函数结合排序规则函数来给取放排序. 2.vector数组的强大功能. #include<iostream> #include< ...
- 性能测试 | 理解单线程的Redis为何那么快?
前言 Redis是一种基于键值对(Key-Value)的NoSQL数据库,Redis的Value可以由String,hash,list,set,zset,Bitmaps,HyperLogLog等多种数 ...
- vacode查看已安装的插件
- selenium webdriver常用函数
from selenium import webdriver driver = webdriver.Ie(executable_path = "e:\\IEDriverServer" ...