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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. [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 ...

  5. 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. ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 洛谷P2634 [国家集训队]聪聪可可 点分治模板

    题意 在一棵树上任意选两个点,求它们距离模3为0的概率. 分析 树分治模板 Code #include<bits/stdc++.h> #define fi first #define se ...

  2. Contos 安装Tomcat

    # 下载安装包 wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.40/bin/apache-tomcat-8. ...

  3. 索引有B+索引和hash索引,各自的区别

    Hash索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B+树索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,那为什么大家不都用Hash索引而还要使用B+树索引呢? ...

  4. python中的break continue之用法

    Break break跳出循环,并且终止最小封闭循环. Continue continue跳过本次循环,继续执行下一次的循环. 二者区别就是break会终止循环,continue不终止循环.

  5. LeetCode 124. 二叉树中的最大路径和(Binary Tree Maximum Path Sum)

    题目描述 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 ...

  6. 微信小程序 图片裁剪

    微信小程序 图片裁剪 分享一个微信小程序图片裁剪插件,很好用,支持旋转 文档:https://github.com/wyh19931106/image-cropper 1.json文件中添加image ...

  7. [Mybatis]查询Sql得到一个字符串

    // find min date HashMap<String, String> minDateMap = new HashMap<String, String>(); min ...

  8. mongodb 安装配置及简单使用

    步骤一: 下载网址:https://www.mongodb.com/download-center/community 根据自己的环境下载 步骤二: 安装过程只需要默认即可,需要注意的是连接工具“mo ...

  9. k8s-helm01-----helm基本使用

    什么是helm Helm 是 Kubernetes 生态系统中的一个软件包管理工具. 基础概念: Helm:客户端,主要负责管理本地的 Charts.repositories 以及与tiller服务器 ...

  10. Mac下安装brew

    1.Mac 终端下,执行以下命令,即可安装brew: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Hom ...