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. ASP.NET发布WebService

    1. 创建一个空的Web应用程序 2. 再添加一个Web服务 3. 在所创建Web服务内,编写一算法 4. 写完可直接运行查看结果 5. 项目->右键,发布此WebService 6.  发布至 ...

  2. iOS开发那些事-iOS6苹果地图实用开发

    在iOS 6之后,不再使用谷歌地图了,而是使用苹果自己的地图,但是API编程接口没有太大的变化.开发人员不需要再学习很多新东西就能开发地图应用,这是负责任的做法.因此本节介绍的内容也同样适用于iOS5 ...

  3. 对索引像素格式的图片进行Setpixel(具有索引像素格式的图像不支持SetPixel)解决方案

    最近编写了一个验证码识别软件.其中对png.jpg图片进行二值化处理时,出现了错误:具有索引像素格式的图像不支持SetPixel解决方案.从字面上来看,这说明我对一个具有索引色的图片进行了直接RGB颜 ...

  4. 20150301—ASP.NET的Repeater

    Repeater与GridView等数据列表一样,都是用来显示数据库的信息的,其中Repeater是最基本的列表形式,其用法也比较灵活. 一.Repeater的位置: 工具箱-数据-Repeater ...

  5. OC7_代理的基本概念

    // // Cat.h // OC7_代理的基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxue ...

  6. 【转载】#274 - Can't Overload if Methods Differ Only by ref and out Modifiers

    You can overload a method in a class, i.e. define two methods with the same name, if the methods hav ...

  7. js中的继承1--类继承

    继承:function Animal(name){ this.name = name; this.showName = function(){ alert(this.name); } } functi ...

  8. CSS样式表与格式布局

    样式表 CSS(Cascading Style Sheets  层叠样式表),作用是美化HTML网页. 内联样式表: 例:<p style="font-size:10px;" ...

  9. Integer类的装箱和拆箱到底是怎样实现的?

    先解释一下装箱和拆箱: 装箱就是  自动将基本数据类型转换为包装器类型:拆箱就是  自动将包装器类型转换为基本数据类型. 下表是基本数据类型对应的包装器类型: int(4字节) Integer byt ...

  10. 1 . Robberies (hdu 2955)

    The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually g ...