Find the second largest element in a Binary Search Tree
来源:互联网 发布:淘宝假台湾回流紫砂壶 编辑:程序博客网 时间:2024/04/28 12:10
find the right most element. If this is a right node with no children, return its parent. if this is not, return the largest element of its left child.
http://www.glassdoor.com/Interview/Find-the-second-largest-element-in-a-Binary-Search-Tree-QTN_238511.htm
Node findSceondLargest(Node root) {
// If tree is null or is single node only, return null (no second largest)
if (root==null || (root.left==null && root.right==null)) return null;
Node parent = null, child = root;
// find the right most child
while (child.right!=null) {
parent = child;
child = child.right;
}
// if the right most child has no left child, then it's parent is second largest
if (child.left==null) return parent;
// otherwise, return left child's rightmost child as second largest
child = child.left;
while (child.right!=null) child = child.right;
return child;
}
- Find the second largest element in a Binary Search Tree
- Search the Nth element of Level M in a binary tree
- Find the nearest common ancestor of any two nodes in a binary search tree
- find the kth largest element in a list and find the first k largest elements in a list
- Largest BST in a Binary Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- Find Mode in Binary Search Tree
- Find Mode in Binary Search Tree
- Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- leetcode501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- Python 虚拟环境:Virtualenv
- Describe 3 kernel functions and when to use which of them
- Leetcode: 4Sum
- 【翻译自mos文章】升级到11.2.0.4之后在alert日志中出现 NUMA 警告信息
- Mushclient 学习计划
- Find the second largest element in a Binary Search Tree
- RHEL 7上能运行的Oracle DB 版本
- 果 的大数据问题
- Leetcode:Letter Combinations of a Phone Number
- C#的130个面試題目大全
- 浮世录----序言
- 【Jason's_ACM_解题报告】Oil Deposits
- Leetcode: Remove Nth Node From End of List
- 读《死于技术 索尼衰亡启示》