find second maximum element in Binary search tree

一个BST, 问怎么找到第二大的节点。首先说了个naive的方法,serialize, 他问有没有更有效的方法。最后用的iterative的方法向右遍历子节点 log(n)

或者inorder, O(n):

 public int find(TreeNode root) {
        TreeNode first = root;
        TreeNode second = null;

        while (root != null) {
            if (root.right == null && root.left == null) {
              return second.val;
            } else if (root.right != null) {
              second = first;
              first = root.right;
              root = root.right;
            } else if (root.left != null) {

              root = root.left;
              break;
            }
        }
        while (root != null) {
          if (root.right == null) {
            return root.val;
          } else {
            root = root.right;
          }
        }
        return -1;
    }

  

时间: 2024-11-17 18:15:07

find second maximum element in Binary search tree的相关文章

[Leetcode] Binary tree -- 501. Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

[LeetCode] Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

173. Binary Search Tree Iterator - Unsolved

https://leetcode.com/problems/binary-search-tree-iterator/#/description Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the B

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and

【LeetCode】Balanced Tree & Binary Search Tree

整合两道差不多的题目放上来,其中第一题是第二题的基础. 1. Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than one. 所谓

LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

Complete Binary Search Tree

本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> Complete Binary Search Tree 1 Question A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than

Find mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod