Reflections on the importance of computer algorithms for system learning

Source: Internet
Author: User

In the process of solving a problem using binary algorithm , it is very simple, do not know the binary algorithm will not be able to find a way to understand the importance of the algorithm.

Topic

Now, let's empty ourselves and look at a topic:

给一棵二叉树,找出从根节点出发到叶节点的路径中,和最大的一条。返回该和。如下,返回4       1           2   3

The examples in the topic are too simple to give a more complicated example:

             1                       6   7          \          3 12 1  2
Thinking of solving problems

If you in high school do not know the "divide and Conquer algorithm", how do you solve this problem? can only say difficult.

The conventional idea is to judge which branch is larger (1->7 branch) every next layer, but this is only for the sake of the present, and there is no guarantee that the sub-tree of the currently selected branch is larger (the branches of 1->7 are smaller than the 1->6 branch). This is good, can only calculate all branches and, finally, compare, this obviously does not.

But if you know the divide-and-conquer algorithm (premise), and you see the idea of a divide-and-conquer algorithm (at the root), the problem is simple (trivial).

According to the idea of divide-and-conquer algorithm, the problem is first "divided" and then "cured".

Divides: divides the tree into the left and right sub-tree (the binary tree is the Pure natural Division structure), calculates the Saozi the maximal path and the tree respectively.
Cure: Compare the size of the Saozi right subtree, take the large value plus your own value.
The whole process is recursive and the code comes out:

    //C++    *root) {        if==NULL)            return0;        = maxPathSum(root->left);        = maxPathSum(root->right);        returnmax+ root->val;    }

The topic for people familiar with the division algorithm is the introduction of the problem, but do not understand the divide and conquer the algorithm, but it is difficult to quickly solve.

Personal notes

Originally written to the top even if the end, but still want to write down their own more ideas, outsiders when personal complaints to see.
1. The divide-and-conquer algorithm does not do actual work in the "minute" process, nor does it give any useful solutions. The solution is given only when the recursion is reached at the lowest level, and then the solution is slowly given by recursive regression steps. (contains a certain abstract meaning)
2. In the process of "governance" is also "with the first step • The absolute trust", the direct treatment of the solution to calculate.
3. The entire process and the "factorial" recursion is basically the same, just a lot of "points" of the procedure.
4. If it is not the problem of the binary tree structure, it may not be so intuitive to use the division rule, which can be used root->left root -> right to represent the left and right branches. However, in the array should be > index able < index to use, to represent.

Off Topic

Link one of the interesting tweets you see today:

"Coders at work" inside, the host asked Douglas, how do you position yourself, scientists or engineers, or artists? ' I'm a writer, sometimes I write in English and sometimes I write in JavaScript, ' says Douglas. What I do is structure to express my thoughts.

Reflections on the importance of computer algorithms for system learning

Related Article

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.