LeetCode summary, leetcode

Source: Internet
Author: User

LeetCode summary, leetcode
The sum of trees is a common topic in the tree. There can be several variants with high flexibility and understanding of the data structure and recursion of the tree. In general, these questions do not need to consider non-recursive solutions (although the truth is the same as the LeetCode Summary-tree traversal, as long as you master it, it should be OK ). In LeetCode, the sum of trees has the following questions:
Path Sum
Path Sum II
Sum Root to Leaf Numbers
Binary Tree Maximum Path Sum

Let's take a look at the most common problem Path Sum. This question is to determine whether the path from the root to the leaf exists and is the same as the given sum. The topic of the tree is basically solved by recursion. There are two main issues to consider: 1) how to divide the problem into subproblems to the left and right Subtrees. Here, we can see whether the left and right subtree exist, and sum minus the value path of the current node. If one exists, the current node has a path. 2) consider what the termination condition is. Here, the end condition is false if the current node is empty. If the other is a leaf, if the remaining sum is equal to the value of the current leaf, find the path that meets the condition and return true. To understand the above two problems, we can implement a tree traversal. According to the analysis just now, we can use parameters or return values to pass the values to be maintained, and then return the results based on Recursive conditions and ending conditions. The algorithm's time complexity is a traversal of O (n), and the space complexity is the size of the stack O (logn ).
For Path Sum II, the idea is exactly the same as Path Sum, but all paths need to be output. Therefore, the data structure is required to maintain the Path and two parameters are added, one path is used to maintain the path to the current node, and the other is used to save all the paths that meet the conditions. The recursive conditions and the ending conditions in the idea are completely consistent, space depends on the number of results.
Sum Root to Leaf Numbers has two more changes. One is that each node is equivalent to a bit value rather than its own weight, but it has not changed much, multiply each layer by 10 and add its own value. Another change is to accumulate all the paths, which is actually to adjust the recursive conditions. In Path Sum, we can determine that there is a Path in the left and right subtree that meets the requirements, here, the result of the left and right subtree is added and returned as the accumulate result of the current node.
The Binary Tree Maximum Path Sum is a big change and a little difficult. The Path of this question is no longer the Path from the root to the leaf. This question is to regard the Tree as an undirected graph, then, find the path. When I think about it, I think it is much more troublesome than the above, but I think it will be well documented. Finding the maximum path of a root node is nothing more than finding the maximum path of the Left subtree and adding my own value, add the maximum path of the right subtree (the path of the left and right subtree may not be retrieved here, if it is smaller than 0 ). The thing we need to do is to accumulate the above for each node. The maximum Path of the Left subtree and the maximum Path of the right subtree are similar to the Path Sum II. Although it does not have to go to the leaf node, the standard is also very simple. If it is greater than 0, if the path goes down and is smaller than 0, it is not obtained. From the perspective of grouping, the maximum path of the left and right subtree is to add Max (0, the maximum path of the Left subtree, and the maximum path of the right subtree) to its own value ). In this case, you don't have to consider so many details. The longest path through the current node is its own value + Max (0, the maximum path of the Left subtree) + Max (0, the maximum path of the right subtree ). Therefore, the entire algorithm is to maintain these two quantities. One is to add the left or right subtree's maximum path as its parent node's intermediate quantity, the other is to add the left and right as the maximum path. For specific implementation, see Binary Tree Maximum Path Sum.
This summary focuses on the questions about the sum of trees in LeetCode. In general, there are three types of summation paths: (1) Root-to-leaf node path; (2) parent node path down the child node; (3) any node to any node (that is, as an undirected graph ). These Path methods often change flexibly during the interview, and the methods for solving the questions in different Path methods are slightly different, but the most complicated is the Path method of Binary Tree Maximum Path Sum, it is still a recursive traversal problem.

What is leetcode?

There are many programming and interview questions, which can be compiled and run online. It is difficult. If you can do it all by yourself, it is very helpful for large companies. I just did the questions there.

C ++ returns a string? Why have I tested both dev c ++ and vc60? But cannot it be used in leetcode?

The Runtime Error does not mean that your program cannot be compiled, but that your program encountered an Error during running. This error is generally caused by out-of-bounds array access, stack overflow, and illegal memory access.

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.