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.

Follow up:
Can you figure out ways to solve it with O(n) time complexity?

分析:

  典型树上的动态规划

代码:

//返回pair中4个值分别代表:是否是BST,BST的节点数,左边界,右边界
pair<pair<bool, int>, pair<int, int>> dfs(TreeNode *cur, int pval, int &maxl) {
pair<int, int> initp(pval, pval);
//为NULL,则返回真,两端值设为父节点的值便于下一步计算
if(!cur)
return make_pair(make_pair(true, ), initp);
//进行下一层遍历
pair<pair<bool, int>, pair<int, int>> leftp, rightp;
leftp = dfs(cur->left, cur->val, maxl);
rightp = dfs(cur->right, cur->val, maxl);
//判断是否为BST
if(leftp.first.first && rightp.first.first && cur->val >= leftp.second.second && cur->val <= rightp.second.first) {
int curlen = leftp.first.second + + rightp.first.second;
maxl = max(maxl, curlen);
return make_pair(make_pair(true, curlen), make_pair(leftp.second.first, rightp.second.second));
}
return make_pair(make_pair(false, ), initp);
}
int largestSubtree(TreeNode *root) {
int maxl = INT_MIN;
dfs(root, , maxl);
return maxl;
}

[Locked] Largest BST Subtree的更多相关文章

  1. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  3. Leetcode: Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  4. [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  5. 333. Largest BST Subtree节点数最多的bst子树

    [抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...

  6. [leetcode]333. Largest BST Subtree最大二叉搜索树子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  7. LeetCode 333. Largest BST Subtree

    原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...

  8. [LeetCode] 333. Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  9. 【LeetCode】333. Largest BST Subtree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

随机推荐

  1. 初识 Angular 体会

    一句话描述:一个前端的类似MVC框架的JS库 刚接触2天,刚一看感觉和asp.net mvc能实现的功能有点重复. 虽然asp.net的表单验证,Razor语法使其在前端开发有较大提升,但要实现比较高 ...

  2. xpath 操作XML

    1.xpath 操作XML,底下部分代码被注释了,但是是完整功能,去除注释是正常使用的(有写命名和其他冲突,所以注释了) 总体有:完整读取xml,对xml的增删改查,对xml的特定操作 using S ...

  3. Android中dip,dp,sp,pt和px的区别

    dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素. ...

  4. Java方法-字符串

    [Java字符串] 通过字符串函数 compareTo (string) ,compareToIgnoreCase(String) 及 compareTo(object string) 来比较两个字符 ...

  5. JAVA JDK 1.6 API中文版.CHM打开chm提示,“ 已取消到该网页的导航”

    JAVA  JDK 1.6 API中文版.CHM打开chm提示,“ 已取消到该网页的导航” silent fish 装了win7后,打开chm文件,发现很多在xp系统打开正常的chm文件竟然出现问题, ...

  6. TOM大师脚本-show space 多个版本,谢谢大牛们

    示例一 该脚本需区分 对象的管理方式是 自动还是 手动, 对手动管理方式 的表显示很全面 SQL> exec show_space_old('MAN_TAB','DEV','TABLE'); F ...

  7. C蛮的全栈之路-序章 技术栈选择与全栈工程师

    目录 C蛮的全栈之路-序章 技术栈选择与全栈工程师C蛮的全栈之路-node篇(一) 环境布置C蛮的全栈之路-node篇(二) 实战一:自动发博客 博主背景 985院校毕业,至今十年C++开发工作经验, ...

  8. Windows 8 / 8.1 禁用驱动签名最详细图文教程

    Windows 8 鼠标右上角/右下角弹出边栏,选择“设置” 点击“更改电脑设置”: 选择“常规”,右侧拖到底,点击“高级启动”-“立即重启”: 稍后进入恢复页面,选择“疑难解答” 进入疑难解答后,选 ...

  9. 【BZOJ2281】【博弈论+DP】 [Sdoi2011]黑白棋

    Description 黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是 ...

  10. 使用gSoap规避和修改ONVIF标准类型结构的解析

    ONVIF/gSoap依赖关系及问题 ONVIF是一组服务规范,标准参考 gSoap是一套基于实现SOAP通信接口的工具链 即是,当我们需要访问ONVIF的Web Service或实现对ONVIF部分 ...