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. JS/jQuery宽高的理解和应用

    1.widows:窗口.window对象可省略 2.document对象是window对象的一部分 浏览器的Html文档成为Document对象 window.location===document. ...

  2. 发现个div float的小秘密

    浮动时宽度塌缩了不再是父元素100%.

  3. w3cschool关于list-style-position时的另外发现

    首先list-style-position有inside和outside... 另外发现:设置inline-block时 那个圆点消失了.. <!DOCTYPE html> <htm ...

  4. Python(2.7.6) 标准日志模块的简单示例

    Python 标准库中的 logging 模块提供了一套标准的 API 来处理日志信息的打印. import logging logging.basicConfig( level = logging. ...

  5. ListView优化-getView优化

    ListView作为Android中最常用的组件之一,其优化方式也比较多. 在使用ListView或是GridView的时候,往往需要自定义数据适配器.一般我们都需要复习getView方法.对于此方法 ...

  6. About Restful Web Api Something.

    这种轻量级的服务架构目前来说还是比较流行的,比如微信的公众平台的接口开发就是一个很好的案例,提到restful那么他到底是一个什么样的东西? REST(Representational State T ...

  7. Game start

    今天开始有计划的码代码吧!!我可是以后要进微软或者google的男人.初步计划先学习编程之美吧,每天码一到题的解法,每天每天每天..然后是ACM竞赛基础,每天一节同上.最后..不对,冷静冷静,我已经没 ...

  8. 【iOS基础学习随笔-1】-基于对象的程序设计

    一.对象: 1.在基于对象的程序设计中,一个程序分解成若干个不同的对象,每个对象都有自己独有的能力. 2.一个生产线上的一个工位只负责做好一件事.如果生产出的汽车的车门没有漆好,那问题很可能出在负责上 ...

  9. javascript数据结构——写一个二叉搜索树

    二叉搜索树就是左侧子节点值比根节点值小,右侧子节点值比根节点值大的二叉树. 照着书敲了一遍. function BinarySearchTree(){ var Node = function(key) ...

  10. javascript笔记——前端实现分页和查询

    //Modal function Modal(obj){ var that = this; that.ref = ""; that.obj = obj; that.init(); ...