637. Average of Levels in Binary Tree Easy
1/**
2 * [637] Average of Levels in Binary Tree
3 *
4 * Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10^-5 of the actual answer will be accepted.
5 *
6 * Example 1:
7 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/avg1-tree.jpg" style="width: 277px; height: 302px;" />
8 * Input: root = [3,9,20,null,null,15,7]
9 * Output: [3.00000,14.50000,11.00000]
10 * Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
11 * Hence return [3, 14.5, 11].
12 *
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/avg2-tree.jpg" style="width: 292px; height: 302px;" />
15 * Input: root = [3,9,20,15,7]
16 * Output: [3.00000,14.50000,11.00000]
17 *
18 *
19 * Constraints:
20 *
21 * The number of nodes in the tree is in the range [1, 10^4].
22 * -2^31 <= Node.val <= 2^31 - 1
23 *
24 */
25pub struct Solution {}
26use crate::util::tree::{TreeNode, to_tree};
27
28// problem: https://leetcode.com/problems/average-of-levels-in-binary-tree/
29// discuss: https://leetcode.com/problems/average-of-levels-in-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
30
31// submission codes start here
32
33// Definition for a binary tree node.
34// #[derive(Debug, PartialEq, Eq)]
35// pub struct TreeNode {
36// pub val: i32,
37// pub left: Option<Rc<RefCell<TreeNode>>>,
38// pub right: Option<Rc<RefCell<TreeNode>>>,
39// }
40//
41// impl TreeNode {
42// #[inline]
43// pub fn new(val: i32) -> Self {
44// TreeNode {
45// val,
46// left: None,
47// right: None
48// }
49// }
50// }
51use std::rc::Rc;
52use std::cell::RefCell;
53impl Solution {
54 pub fn average_of_levels(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<f64> {
55 vec![]
56 }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_637() {
67 }
68}
69
Back
© 2025 bowen.ge All Rights Reserved.