LeetCode summary, leetcode

Source: Internet
Author: User

LeetCode summary, leetcode
The decision on the nature of the tree is the basic operation of the tree's data structure. It is generally a very simple question, that is, the first entry question. The most difficult question in the interview, do not give the interviewer any chance to pick a thorn. LeetCode has the following questions about the nature of the tree:
Maximum Depth of Binary Tree
Minimum Depth of Binary Tree
Balanced Binary Tree
Same Tree
Symmetric Tree

First, let's talk about the question about how to find the Depth of the Tree. The simplest thing is to find the Maximum Depth of Binary Tree, which is generally implemented using recursion. The idea is simple. You only need to return 0 to the left and right nodes, and then increment the values of the other values by layer. You can just take the large depth in the left and right subtree. Minimum Depth of Binary Tree is a little more complex, mainly because it takes a small Depth of left and right subtree, but one case is not counted in Depth, for example, if the left subtree is completely empty, we do not think that the depth is 0 because there are no leaves on the left. We need to find the minimum depth of the leaf node according to the definition. Therefore, you need to make an additional judgment on whether the left and right are null. The depth of the tree is a simple question, so if Recursive Implementation is faster, the interviewer may ask how non-recursive implementation is achieved. If you have time, you have to practice it, the principle is consistent with the LeetCode Summary-tree traversal.
The Balanced Binary Tree is an extension question for depth. The basic principle is to find depth. However, we need to add a step to determine whether it is a Balance Tree, because the depth is the amount that we must maintain. If we use an extra Boolean variable to maintain whether it is a balance tree, we can also. However, here we can use the nature of depth greater than 0 to return the normal depth value to the balanced tree, while the-1 is returned for the imbalance to distinguish, this is equivalent to using a variable to maintain the desired two properties, and the code implementation is relatively simple.
Same Tree is also a basic topic. It is the Same as the time span of the Tree. It only performs the Same traversal for the two trees at the Same time and then compares them one by one. If there is a difference, false is returned.
The secondary Ric Tree will be slightly bypassed, but it is still similar to the Same Tree. The first difference is that it should be compared based on the left and right Subtrees, in fact, the left and right subtree can be regarded as two trees in the Same Tree. The second difference is that in the recursive process, the left and right Subtrees of the nodes are exchanged and compared, that is, the ratio of left to right and the ratio of right to left.
This article mainly mentions some basic questions about tree search in LeetCode. This type of questions is relatively simple and belongs to the lowest threshold. Therefore, we should try our best to implement the bug free.

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.