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. 使用OLEDB读取excel和csv文件

    这是我第一次在博客上写东西,简单的为大家分享一个oledb读取文件的功能吧,这两天在做一个文件导入数据库的小demo,就想着导入前先在页面上展示一下,之前调用Microsoft.Office.Inte ...

  2. Layabox:全球唯一Flash直接开发H5的工具Laya.Flash终于开放(转)

    HTML5是风口,但年轻幼稚,Flash昔日黄花,但丰满妖娆,Layabox使用Flash直接开发HTML5(或编译页游),渴望性感而青春,但久在深闺,只见于一些大企业和朋友圈中,被指“饥饿营销”,保 ...

  3. public animal this[int index]|索引器的使用

    学习如何使用索引器,索引器的使用是public 类型 this[int index]{get{};set{}} ,访问通过类的实例(对象)加[i], 例如animal[i],就像访问数组一样,其实就是 ...

  4. SERVER 2012 R2 core域环境下批量创建用户

      Write by xiaoyang 转载请注明出处 步骤一:创建域 基本配置 1.         输入命令进入配置 2.         输入8进入网络配置 3.         选择要配置的网 ...

  5. 20141017--循环语句for 穷举

    穷举:把所有的可能性都列举一遍 1.羽毛球怕15元一个,球3元一个,水2元一瓶,一共有200元,每种至少一个,列出所有可能: 2.   50元钱,有面值2元,3元,5元,不要求每种至少一张,有多少种组 ...

  6. java静态代码块、初始化块和构造方法的执行顺序

    分析:当执行new Child()时,它首先去看父类里面有没有静态代码块,如果有,它先去执行父类里面静态代码块里面的内容,当父类的静态代码块里面的内容执行完毕之后,接着去执行子类(自己这个类)里面的静 ...

  7. 利用CSS3打造一组质感细腻丝滑的按钮

    CSS3引入了众多供功能强大的新特性,让设计和开发人员能够轻松的创作出各种精美的界面效果.下面这些发出闪亮光泽的按钮,漂亮吧?把鼠标悬停在按钮上,还有动感的光泽移动效果. 温馨提示:为保证最佳的效果, ...

  8. Java Dao模式通过JDBC连接数据库的操作

    Java程序访问数据库: 1.获取数据库厂商提供的驱动(jdbc接口的实现类) 如ojdbc14.jar——Oracle数据库驱动jar包 mysql-connector-java-5.1.8-bin ...

  9. MySQL 5.7 Zip 安装(win7)

    参考官方文档 http://dev.mysql.com/doc/refman/5.7/en/windows-install-archive.html 2.3.5.1 Extracting the In ...

  10. AD查询1000条限制和解决方案

      公司的一个项目要从AD上取数据,为了测试性能,批量在AD上创建了2000多个用户.但是用java程序获取所有用户的时候会报错或者只能取到1000条数据.   条数据. 用org.springfra ...