In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Java how to find the width of the tree", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Java how to find the width of the tree" bar!
Import com.lifeibigdata.algorithms.leetcode.TreeNode;import java.util.ArrayDeque;import java.util.Queue;public class TreeWidth {/ * uses queues to traverse the binary tree hierarchically. After traversing the previous layer, all the nodes of the next layer have been placed in the queue, and the number of elements in the queue is the width of the next layer. * and so on, the maximum width of the binary tree can be obtained by traversing the next layer * @ param root * @ return * / static int getTreeWidth (TreeNode root) {if (root = = null) return 0; Queue queue = new ArrayDeque (); int maxWidth = 0; queue.add (root); while (true) {/ / int len = queue.size () If (len = = 0) break; while (len > 0) {/ / ensures that the upper layer is traversed, so the len variable TreeNode t = queue.poll (); len--; if (t.left! = null) queue.add (t.left); if (t.right! = null) queue.add (t.right) } maxWidth = Math.max (maxWidth,queue.size ());} return maxWidth;}} Thank you for your reading. The above is the content of "how Java works out the width of the tree". After the study of this article, I believe you have a deeper understanding of how Java works out the width of the tree, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.