Leetcode: Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:
A subtree must include all of its descendants.
Here's an example:
10
/ \
5 15
/ \ \
1 8 7
The Largest BST Subtree in this case is the highlighted one.
The return value is the subtree's size, which is 3.
Hint: You can recursively use algorithm similar to 98. Validate Binary Search Tree at each node of the tree, which will result in O(nlogn) time complexity.
Follow up:
Can you figure out ways to solve it with O(n) time complexity?
refer to https://discuss.leetcode.com/topic/36995/share-my-o-n-java-code-with-brief-explanation-and-comments/2
这道题不好从root到leaf一层一层限制subtree取值范围,因为有可能parent并不能构成BST,反倒是需要如果subtree是BST的话,限制parent的取值范围,然后根据情况判断是:
1. 吸纳parent以及parent的另一个subtree进来形成一个更大的BST,向上传递这个新subtree的size
2. parent向上传递自它以下发现的最大BST
所以我们需要传递subtree size, subtree (min, max)范围,所以我们想到用一个wrapper class包括这三项东西。
同时因为要能分辨上面1、2两种情况,所以size为正表示1,size为负表示2
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public class Result {
int res;
int min;
int max;
public Result(int num, int n1, int n2) {
this.res = num;
this.min = n1;
this.max = n2;
}
} public int largestBSTSubtree(TreeNode root) {
Result res = findLargestBST(root);
return Math.abs(res.res);
} public Result findLargestBST(TreeNode cur) {
if (cur == null) return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE);
Result left = findLargestBST(cur.left);
Result right = findLargestBST(cur.right);
if (left.res<0 || right.res<0 || cur.val<left.max || cur.val>right.min) {
return new Result(Math.max(Math.abs(left.res), Math.abs(right.res))*(-1), 0, 0);
}
else return new Result(left.res+right.res+1, Math.min(left.min, cur.val), Math.max(right.max, cur.val));
}
}
Leetcode: Largest BST Subtree的更多相关文章
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- LeetCode 333. Largest BST Subtree
原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...
- [LeetCode] 333. Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [leetcode]333. Largest BST Subtree最大二叉搜索树子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- 【LeetCode】333. Largest BST Subtree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [Locked] Largest BST Subtree
Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary Search Tree (BST ...
- 333. Largest BST Subtree节点数最多的bst子树
[抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...
随机推荐
- Codevs p1004 四子连棋
四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...
- 【异常】java.lang.LinkageError: loader constraints violated
[问题背景] 南非客户帐单提醒功能过程中的一个问题,当启动服务器后,后台报java.lang.LinkageError: loader constraints violated when l ...
- BZOJ4491: 我也不知道题目名字是什么
Description 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 Input 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一 ...
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefaults是首选.下次再登陆的时候就可以直接从NSUserDefa ...
- 在docker 中搭建gitlab环境
docker run --name gitlab -it -p : -p : -p : \ --env 'GITLAB_SECRETS_DB_KEY_BASE=Rpwq35wjLJ5N6CrkvdXs ...
- Mac 下安装PHP遇到的问题
checking for CRYPTO_free in -lcrypto... no configure: error: libcrypto not found!http://www.openssl. ...
- jsp页面价格
<div class='flo_left'> <h2 class='red'>订货价¥${productEntity.agentsPrice}</h2> <h ...
- 1019 JDBC链接数据库进行修删改查
package com.liu.test01; import java.sql.Statement; import java.sql.Connection; import java.sql.Drive ...
- Xilinx SDK Problem Solution in Ubuntu
Problem1: Documention and Example can't open, Xilinx SDK Ubuntu. Step1: Click the Document link o ...
- 将CachedRowSet中的数据转储到对象中
虽然还有很多bug,但凑合能用,就是将CachedRowSet中的数据转换成对象或List.省去了繁琐难看的一系列get/set方法. 先说调用: 注: cachedRowSet是查询的结果集 Stu ...