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.的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. 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. ...

  5. [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 ...

  6. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

  7. [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 ...

  8. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  9. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

随机推荐

  1. IDG合伙人李丰:O2O中的C2C蕴藏巨大商机

    [ 亿欧导读 ] IDG合伙人李丰表示,每个新趋势出现,都是在解决上一轮行业革新时没有解决好的市场需求.而O2O中的C2C将会出现巨大商机的原因在于移动互联网的出现创造了新的交互方式,可以更快速的匹配 ...

  2. Table of Contents - Nginx

    Downloading and  Installing Nginx Nginx for Windows Basic Nginx Configuration Configuration File Syn ...

  3. jQuery无缝循环开源多元素动画轮播jquery.slides插件

    详细内容请点击 初始化插件: 一款基于jQuery无缝轮播图插件,支持图内元素动画,可以自定义动画类型$(".slideInner").slide({slideContainer: ...

  4. Android微信支付SDK开发笔记

    一.准备工作 1.开发平台及SDK下载 微信开放平台 https://open.weixin.qq.com 下载SDK 微信支付Demo下载 http://pay.weixin.qq.com/wiki ...

  5. 设置WinForm窗体及程序图标

    自己留着看,总是用的时候给忘记了,百度来百度去的麻烦. 设置 Ico 图标为 [资源文件] 项目名à右键à属性,在选项卡中选择"资源"   选择 "添加资源"à ...

  6. iOS使用keychain存储密码

    iOS设备中的Keychain是一个安全的存储容器.通常情况下,可以用NSUserDefaults存储数据信息,但是对于一些私密信息,比如账号.密码等等,就需要使用更为安全的keychain了.苹果自 ...

  7. cocos2d-x中Node与Node层级架构

    Cocos2d-x采用层级(树形)结构管理场景.层.精灵.菜单.文本.地图和粒子系统等节点(Node)对象.一个场景包含了多个层,一个层又包含多个精灵.菜单.文本.地图和粒子系统等对象.层级结构中的节 ...

  8. iOS-Andriod百度地图仿百度外卖-饿了么-选择我的地址-POI检索/

    http://zanderzhang.gitcafe.io/2015/09/19/iOS-Andriod百度地图仿百度外卖-饿了么-选择我的地址-POI检索/ 百度外卖选择送货地址: 饿了么选择送货地 ...

  9. AMQ学习笔记 - 12. Spring-JmsTemplate特性设置

    概述 这是关于JmsTemplate的最后一篇总结,且只会介绍几个比较重要的特性. 消息的递送模式 在发送消息给时,可以告知这是持久化的消息,还是非持久化的消息.如果是非持久化的消息,broker会将 ...

  10. AMQ学习笔记 - 14. 实践方案:基于ZooKeeper + ActiveMQ + replicatedLevelDB的主从部署

    概述 基于ZooKeeper + ActiveMQ + replicatedLevelDB,在Windows平台的主从部署方案. 主从部署可以提供数据备份.容错[1]的功能,但是不能提供负载均衡的功能 ...