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. jquery 银行卡号验证

    具体参考:https://github.com/jondavidjohn/payform 插件js: jquery.payform.js 具体操作 alert($.payform.validateCa ...

  2. IDEA 13》》》14破解

    更新IDEA 15注册方式 http://15.idea.lanyus.com/ ------------------------------------------------ 之前的已不能用,下面 ...

  3. UIAlertController (UIActionSheet, UIAlertView is deprecated in iOS 8.)

    iOS 8 后 UIAlertView 和  UIActionSheet 都被合并到了 UIAlertController里面. 文档原文: Important: UIAlertView is dep ...

  4. elastic search 学习笔记

    Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...

  5. 【BZOJ2038】【莫队】小z的袜子

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  6. PHP中的&传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么?

    PHP中的&传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么? 代码如下: <?php $arr = array('one','two','three'); fo ...

  7. PHP android ios相互兼容的AES加密算法

    APP项目用户密码传输一直没有用HTTPS,考虑到用户的隐私暂时先用AES对密码加密,以后也可以用于手机端与服务端加密交互. PHP的免费版phpAES项目,手机端解码各种不对. 好不容易找了PHP ...

  8. ECSHOP错误Redefining already defined constructor for class如何解决

    本地PHP环境PHP5.4,安装ecshop2.7.3后,很多地方会报如下的错 Redefining already defined constructor for class XXX 使用和类名相同 ...

  9. bom type:Phantom

    bom的类型 'type': fields.selection([('normal','Normal BoM'),('phantom','Sets / Phantom')], 'BoM Type', ...

  10. C语言学习笔记(二):指针的用法

    与其说指针是一种工具,不如先说指针是一种数据类型. -------------------------------------------------------------华丽的分割线------- ...