Lis Problem Analysis

Source: Internet
Author: User

Source of question: "waiting for words", original @ Chen liren. You are welcome to continue to pay attention to the Public Account "" waiting for words"

The original question is Lis, not longest increasing subsequence, But largest independent set. Meaning: Given a binary tree, find the largest Node Set meeting the following conditions: any two nodes in the set, no edge. For example:


The LIS size is 5, which is {10, 40, 60, 70, 80 }.

Analysis: the first is recursive thinking. For example, for root node 10, if 10 is put into Lis, 20 and 30 cannot be put, but their children can be 40, 50, 60; if you do not place 10 in it, then its Child 20 and 30 can, so the recursive equation can be obtained

Fun (Root) = max (fun (root-> left) + fun (root-> right), 1 + fun (root-> left) + fun (root-> left-> right) + fun (root-> right-> left) + fun (root-> right); likewise, due to the replay problem, you can consider dynamic planning. But for the tree, dynamic planning requires special processing, because we cannot process it from the bottom up. Here is a skill, we can add a flag LIS to the Tree node. When the node is not accessed, the lis is initialized to 0. After the access, the lis is saved as the size of the LIS set of the current node, thus avoiding repeated recursion, for details, see the code:

Struct binarytreenode {int data; int Lis; // indicates the size of the LIS set at the root of the current node. The initial value is 0, which prevents repeated recursion of binarytreenode * left; binarytreenode * right; binarytreenode (INT Val): Data (VAL), Lis (0), left (null), right (null) {}}; int Lis (binarytreenode * root) {If (root = NULL) return 0; If (root-> LIS) return root-> Lis; int numnotcontainroot = Lis (root-> left) + Lis (root-> right); // lisint numcontatinroot = 1 is not added to the root node; // lisif (root-> left) is added to the root node) numcontatinroot + = Lis (root-> left) + Lis (root-> left-> right); If (root-> right) numcontatinroot + = Lis (root-> right-> left) + Lis (root-> right); root-> Lis = max (numnotcontainroot, numcontatinroot ); // Save the LIS size of the subtree of the current root node. Return root-> Lis ;}

This Code only represents my opinion. If you have any mistakes, please correct them. Thank you.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.