Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use binary tree

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report