Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use binary tree

Shulou Source: shulou.com Published: 2022-06-03 00:51:51 10月02日 Update

This article mainly introduces "how to use binary tree". In daily operation, I believe many people have doubts about how to use binary tree. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use binary tree"! Next, please follow the editor to study!

"the following previous order traversal is taken as an example:"

"determine the parameters and return value of the recursive function": to print out the value of the preorder traversal node, you need to input the value of vector in the parameter. In addition to this, there is no need to process any data and no return value is required, so the return type of the recursive function is void. The code is as follows:

Void traversal (TreeNode* cur, vector& vec)

"determine the termination condition": in the process of recursion, how does the recursion end? of course, if the current traversal node is empty, then this layer recursion is about to end, so if the current traversal node is empty, return directly. The code is as follows:

If (cur = = NULL) return

"determine the logic of single-layer recursion": pre-order traversal is about the order in the middle, so the logic of single-layer recursion is to take the value of the middle node first, as follows:

Vec.push_back (cur- > val); / medium traversal (cur- > left, vec); / / left traversal (cur- > right, vec); / / right

The logic of single-layer recursion is processed in the left and right order, so that the preorder traversal of the binary tree is almost finished. Take a look at the complete code:

Preorder traversal:

Class Solution {public: void traversal (TreeNode* cur, vector& vec) {if (cur = = NULL) return; vec.push_back (cur- > val); / medium traversal (cur- > left, vec); / / left traversal (cur- > right, vec); / / right} vector preorderTraversal (TreeNode* root) {vector result; traversal (root, result); return result }}

Then after the preorder traversal is written, it is not difficult to understand the mid-order and post-order traversal. The code is as follows:

Traversal in the middle order:

Void traversal (TreeNode* cur, vector& vec) {if (cur = = NULL) return; traversal (cur- > left, vec); / / left vec.push_back (cur- > val); / / Middle traversal (cur- > right, vec); / / right}

Traversal in the following order:

Void traversal (TreeNode* cur, vector& vec) {if (cur = = NULL) return; traversal (cur- > left, vec); / / left traversal (cur- > right, vec); / / right vec.push_back (cur- > val); / / medium}

At this point, you can do the three questions on leetcode, which are:

one hundred and forty four。 Preorder traversal of binary tree

one hundred and forty five。 Post-order traversal of binary trees

ninety-four。 Mid-order traversal of binary trees

At this point, the study on "how to use binary tree" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Recursion code node learning single layer numerical value logic center-left function parameter that is more processing help practical and then done data article method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno MySQL MariaDB NVidia