Interview-Largest independent set in binary tree.
BT(binary tree), want to find the LIS(largest independent set) of the BT. LIS: if the current node is in the set, then its children should not be in the set. So that the set has the largest number of nodes.
Analysis:
Recursion method. Return the LIS of current root. Argument include whether the father of current root is in the LIS.
1. Father of root is not in LIS, then we have two choice:
1.1 current root in LIS, then we recursively get LIS(root, father not in) = LIS(root.left, root in LIS) + LIS(root.right, root in LIS) + 1.
1.2. current root not in LIS, then we recursively get LIS(root,father not in) = LIS(root.left, root not in) +LIS(root.right, root not in).
we then return the larger one.
2. Father of root is in LIS, then we only have on choice:
current root not in LIS, then we recursively get LIS(root,father not in) = LIS(root.left, root not in) +LIS(root.right, root not in).
NOTE: we notice that this recursive method has a lot of repeated calculation. we can use memorized search.
Return: {LIS(root in), LIS(root not in)}.
For each current root, we calculate {LIS{root.left in), LIS(root.left not in)} and {LIS{root.right in), LIS(root.right not in)}
Then we have LIS(root in) = Lis(root.left not in)+Lis(root.right not in)+1.
LIS(root not in) = max{LIS{root.left in), LIS(root.left not in)} + max{LIS{root.right in), LIS(root.right not in)}
Interview-Largest independent set in binary tree.的更多相关文章
- Cracking the Code Interview 4.3 Array to Binary Tree
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal hei ...
- [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- LintCode Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- [geeksforgeeks] Convert a given Binary Tree to Doubly Linked List
http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Bin ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
- [Swift]LeetCode998. 最大二叉树 II | Maximum Binary Tree II
We are given the root node of a maximum tree: a tree where every node has a value greater than any o ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
随机推荐
- 为Debug和Release分别设置Web.config
需求:在开发asp.net应用程序时,往往想在debug和release环境下使用不同的配置,而web.config文件却只有一个 解决方案:可以在原来的web.config中写下debug环境下的配 ...
- Flask Restful Small Demo
参考: http://www.pythondoc.com/flask-restful/first.html 什么是Rest Client-Server:服务器端与客户端分离. Stateless(无状 ...
- XenCenter删除SR失败解决方法
到CLI下 查SR的UUID xe sr-list SR的uuid=e0571e72-f6c5-1c9e-4ad8-9817b2331f47 FORGET SR xe sr-forget uuid=e ...
- php 检查email电子邮件函数(奇葩写法)
以前写的一个PHP表单电子邮件发送程序,其中采用如下方法来验证电子邮件地址格式是否正确: 代码如下 复制代码 eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0 ...
- Android数据库之SQLite数据库
Android数据库之SQLite数据库 导出查看数据库文件 在android中,为某个应用程序创建的数据库,只有它可以访问,其它应用程序是不能访问的,数据库位于Android设备/data/data ...
- @@ROWCOUNT (Transact-SQL)
@@ROWCOUNT (Transact-SQL) 返回受上一语句影响的行数.如果行数大于 20 亿,请使用 ROWCOUNT_BIG. Transact-SQL 语法约定 语法 @@ROWCOUNT ...
- jquery动态移除/增加onclick属性详解
本文章给大家介绍利用jquery的removeAttr与attr事件来给a标签增加与删除onclick事件的具体操作方法,有需要了解的朋友可参考. 要实现效果:点击链接先去掉onclick属性,3秒 ...
- (转)在Windows上以服务方式运行 MSOPenTech/Redis
ServiceStack.Redis 使用教程里提到Redis最好还是部署到Linux下去,Windows只是用来做开发环境,现在这个命题发生改变了,在Windows上也可以部署生产环境的Redis, ...
- python——异常处理
python处理异常的格式一般如下: try: A部分 Except .... [as ..]: B部分 [ finally: C部分 Else: D部分 ] ps: 1.不管有没有异常,final ...
- 比较不错的一个ios找茬游戏源码
找茬游戏源码 ,这个是一款非常不错的ios找茬游戏源码,该游戏的兼容性非常好的,并且还可以支持ipad和iphone,UI界面设计得也很漂亮,游戏源码真的是一款非常完美,而且又很完整的一款休闲类的游戏 ...