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)}

时间: 2024-12-20 01:30:50

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 height. 1.Divide the array equally into left part and right part, the mid value will be the root. 2.Recall the function to the left and right pat of the

Binary Tree Path Sum Lintcode

Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target. A valid path is from root node to any of the leaf nodes. Have you met this question in a real interview? Yes Example Given a binary tree, and targe

[LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Have you met this question in a real interview? Yes Example Given a binary tree as follow:

Cracking the Coding Interview, Binary Tree, Binary Search Tress

Binary Tree: 0到2个子节点; Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点: 1. Full类型: 除最下面一层外, 每一层都有两个子节点: 2. Complete类型: 除最下面一层外为Full类型, 但是最下面一层最所有子节点靠左: 3. Balanced类型: 左右两个子树的长度相差小于等于一: traverse: 遍历: recursion: 递归(反复用自身): iteration: 迭代(循环): 3种遍历: 1.

[LeetCode][Java] 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. For example: Given the below binary tree, 1 / 2 3 Return 6. 题意: 给定一棵二叉树,找出最大得路径和. 路径的起始和结束位置可以是树中的任意节点. 比如, 给定如下的一棵二叉树 1 / 2 3 返回  6. 算法分析: 1) Rec

LeetCode: Binary Tree Postorder Traversal 解题报告

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively

LeetCode 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h

LeetCode 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal My Submissions Question Total Accepted: 108336 Total Submissions: 278322 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3

LeetCode_Construct Binary Tree from Preorder and Inorder Traversal

一.题目 Construct Binary Tree from Preorder and Inorder Traversal Total Accepted: 36475 Total Submissions: 138308My Submissions Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist