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 ...
随机推荐
- IO输入输出 2
编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt pa ...
- oracle中的instr()
INSTR (源字符串, 目标字符串, 起始位置, 匹配序号) 在Oracle/PLSQL中,instr函数返回要截取的字符串在源字符串中的位置.只检索一次,就是说从字符的开始 到字符的结尾就结束. ...
- Android聊天界面刷新消息
今天,我想来分享一下自己初用线程的感受,虽然写法略显粗糙,并没有用线程Thread中核心的Looper,MessageQueue消息队列这些知识,正因为是初学线程,所以就只用最基础的来写了,慢慢学习优 ...
- Document.getElementById 与 $('#id')的区别
一直认为jquery中的$("#id")和document.getElementByIdx_x("id")得到的效果是一样的,今天才发现并不是这么一回事,通过测 ...
- Android之图片颜色调节
package net.hnjdzy.imagecolor; import android.os.Bundle; import android.app.Activity; import android ...
- MVC 构造
// // View.h // UI5_HomeWork // // Created by zhangxueming on 15/7/2. // Copyright (c) 2015年 zhangxu ...
- wix xslt for adding node
Using xslt to add new node item to wix source code. Original wix code: <Fragment> <Director ...
- const关键字在C和C++区别
1)C++默认为内部链接:C默认为外部链接2)在C++中,一般一个const不会创建内存空间,而是将其保存在符号表(待看).比如: ; char buf[bufsize]; 这里无需为const创建内 ...
- 《RHEL6硬盘的分区和swap分区管理》——硬盘分区的大总结
首先介绍下几个简单的命令: free查看当前系统内存的使用情况 查看分区的使用情况:T类型.H显示大小以G,M 查看系统所有硬盘的分区信息:分区的没分区的都显示出来了 开始分区:为什么要加cu 不加 ...
- Ubuntu16.04.1 安装Redis-Cluster
Redis在3.0版正式引入了集群这个特性.Redis集群是一个分布式(distributed).容错(fault-tolerant)的 Redis内存K/V服务, 集群可以使用的功能是普通单机 Re ...