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

What is the layer average of python binary tree?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about what the average layer of the python binary tree is. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Order

Mainly record the layer average of python binary tree

Given a non-empty binary tree, the topic returns an array of average nodes at each layer. Example 1: input: 3 /\ 9 20 /\ 157 output: [3,14.5,11] explain: the average value of layer 0 is 3, layer 1 is 14.5, and layer 2 is 11. So return [3, 14.5, 11]. Tip: node values are within the range of 32-bit signed integers. Source: LeetCode link: https://leetcode-cn.com/problems/average-of-levels-in-binary-tree copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. Answer the question / Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x;} *} * / class Solution {public List averageOfLevels (TreeNode root) {if (root = = null) {return Collections.emptyList ();} List result = new ArrayList (); Queue queue = new LinkedList (); queue.offer (root) While (! queue.isEmpty ()) {int size = queue.size (); double sum = 0; for (int iTuno; I < size; iTunes +) {TreeNode node = (TreeNode) queue.poll (); sum + = node.val; if (node.left! = null) {queue.offer (node.left) } if (node.right! = null) {queue.offer (node.right);}} result.add (sum*1.0/size);} return result;}}

Here, hierarchical traversal is carried out with the help of the queue. Each time, record the size of the queue, then press size to poll, take out the elements to accumulate the sum, and then put the node.left and node.right that are not null into the queue, and finally calculate the sum/size into the result.

The above is the average layer of the python binary tree shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report