In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve hierarchical traversal of binary tree by java". In daily operation, I believe that many people have doubts about how to achieve hierarchical traversal of binary tree by java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to achieve hierarchical traversal of binary tree by java". Next, please follow the editor to study!
Title
Given a binary tree, returns the node value that it traverses in a hierarchy. (that is, access all nodes layer by layer, from left to right.)
For example:
Given binary tree: [3, 9, 20, 10, 15, 7]
Returns the result of its hierarchical traversal:
[
[3],
[9,20]
[15,7]
]
Problem-solving ideas
This problem requires the use of queues
Create a queue
First put the root node in, then find the left and right child nodes of the root node.
Remove the root node, and the elements in the queue are all the nodes in the next layer
Iterate through the for loop and store the results in an one-dimensional vector
After traversing, save this one-dimensional vector into a two-dimensional vector.
By analogy, sequence traversal can be completed.
Animation demonstration
GIF is a little slow to load in the animation. Please wait a moment ^ _ ^
Reference code
At this point, the study of "how to achieve hierarchical traversal of binary trees by java" 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un