|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 中等 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3157.Find%20the%20Level%20of%20Tree%20with%20Minimum%20Sum/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3157. Find the Level of Tree with Minimum Sum 🔒](https://leetcode.cn/problems/find-the-level-of-tree-with-minimum-sum) |
| 10 | + |
| 11 | +[English Version](/solution/3100-3199/3157.Find%20the%20Level%20of%20Tree%20with%20Minimum%20Sum/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>Given the root of a binary tree <code>root</code> where each node has a value, return the level of the tree that has the <strong>minimum</strong> sum of values among all the levels (in case of a tie, return the <strong>lowest</strong> level).</p> |
| 18 | + |
| 19 | +<p><strong>Note</strong> that the root of the tree is at level 1 and the level of any other node is its distance from the root + 1.</p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | + |
| 24 | +<div class="example-block"> |
| 25 | +<p><strong>Input:</strong> <span class="example-io">root = [50,6,2,30,80,7]</span></p> |
| 26 | + |
| 27 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 28 | + |
| 29 | +<p><strong>Explanation:</strong></p> |
| 30 | + |
| 31 | +<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3157.Find%20the%20Level%20of%20Tree%20with%20Minimum%20Sum/images/image_2024-05-17_16-15-46.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 265px; height: 129px;" /></p> |
| 32 | +</div> |
| 33 | + |
| 34 | +<p><strong class="example">Example 2:</strong></p> |
| 35 | + |
| 36 | +<div class="example-block"> |
| 37 | +<p><strong>Input:</strong> <span class="example-io">root = [36,17,10,null,null,24]</span></p> |
| 38 | + |
| 39 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 40 | + |
| 41 | +<p><strong>Explanation:</strong></p> |
| 42 | + |
| 43 | +<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3157.Find%20the%20Level%20of%20Tree%20with%20Minimum%20Sum/images/image_2024-05-17_16-14-18.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 170px; height: 135px;" /></p> |
| 44 | +</div> |
| 45 | + |
| 46 | +<p><strong class="example">Example 3:</strong></p> |
| 47 | + |
| 48 | +<div class="example-block"> |
| 49 | +<p><strong>Input:</strong> <span class="example-io">root = [5,null,5,null,5]</span></p> |
| 50 | + |
| 51 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 52 | + |
| 53 | +<p><strong>Explanation:</strong></p> |
| 54 | + |
| 55 | +<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3157.Find%20the%20Level%20of%20Tree%20with%20Minimum%20Sum/images/image_2024-05-19_19-07-20.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 170px; height: 135px;" /></p> |
| 56 | +</div> |
| 57 | + |
| 58 | +<p> </p> |
| 59 | +<p><strong>Constraints:</strong></p> |
| 60 | + |
| 61 | +<ul> |
| 62 | + <li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li> |
| 63 | + <li><code>1 <= Node.val <= 10<sup>9</sup></code></li> |
| 64 | +</ul> |
| 65 | + |
| 66 | +<!-- description:end --> |
| 67 | + |
| 68 | +## 解法 |
| 69 | + |
| 70 | +<!-- solution:start --> |
| 71 | + |
| 72 | +### 方法一:BFS |
| 73 | + |
| 74 | +我们可以使用 BFS,逐层遍历二叉树,记录每一层的节点值之和,找到具有最小节点值之和的层,返回该层的层数。 |
| 75 | + |
| 76 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。 |
| 77 | + |
| 78 | +<!-- tabs:start --> |
| 79 | + |
| 80 | +#### Python3 |
| 81 | + |
| 82 | +```python |
| 83 | +# Definition for a binary tree node. |
| 84 | +# class TreeNode: |
| 85 | +# def __init__(self, val=0, left=None, right=None): |
| 86 | +# self.val = val |
| 87 | +# self.left = left |
| 88 | +# self.right = right |
| 89 | +class Solution: |
| 90 | + def minimumLevel(self, root: Optional[TreeNode]) -> int: |
| 91 | + q = deque([root]) |
| 92 | + ans = 0 |
| 93 | + level, s = 1, inf |
| 94 | + while q: |
| 95 | + t = 0 |
| 96 | + for _ in range(len(q)): |
| 97 | + node = q.popleft() |
| 98 | + t += node.val |
| 99 | + if node.left: |
| 100 | + q.append(node.left) |
| 101 | + if node.right: |
| 102 | + q.append(node.right) |
| 103 | + if s > t: |
| 104 | + s = t |
| 105 | + ans = level |
| 106 | + level += 1 |
| 107 | + return ans |
| 108 | +``` |
| 109 | + |
| 110 | +#### Java |
| 111 | + |
| 112 | +```java |
| 113 | +/** |
| 114 | + * Definition for a binary tree node. |
| 115 | + * public class TreeNode { |
| 116 | + * int val; |
| 117 | + * TreeNode left; |
| 118 | + * TreeNode right; |
| 119 | + * TreeNode() {} |
| 120 | + * TreeNode(int val) { this.val = val; } |
| 121 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 122 | + * this.val = val; |
| 123 | + * this.left = left; |
| 124 | + * this.right = right; |
| 125 | + * } |
| 126 | + * } |
| 127 | + */ |
| 128 | +class Solution { |
| 129 | + public int minimumLevel(TreeNode root) { |
| 130 | + Deque<TreeNode> q = new ArrayDeque<>(); |
| 131 | + q.offer(root); |
| 132 | + int ans = 0; |
| 133 | + long s = Long.MAX_VALUE; |
| 134 | + for (int level = 1; !q.isEmpty(); ++level) { |
| 135 | + long t = 0; |
| 136 | + for (int m = q.size(); m > 0; --m) { |
| 137 | + TreeNode node = q.poll(); |
| 138 | + t += node.val; |
| 139 | + if (node.left != null) { |
| 140 | + q.offer(node.left); |
| 141 | + } |
| 142 | + if (node.right != null) { |
| 143 | + q.offer(node.right); |
| 144 | + } |
| 145 | + } |
| 146 | + if (s > t) { |
| 147 | + s = t; |
| 148 | + ans = level; |
| 149 | + } |
| 150 | + } |
| 151 | + return ans; |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +#### C++ |
| 157 | + |
| 158 | +```cpp |
| 159 | +/** |
| 160 | + * Definition for a binary tree node. |
| 161 | + * struct TreeNode { |
| 162 | + * int val; |
| 163 | + * TreeNode *left; |
| 164 | + * TreeNode *right; |
| 165 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 166 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 167 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 168 | + * }; |
| 169 | + */ |
| 170 | +class Solution { |
| 171 | +public: |
| 172 | + int minimumLevel(TreeNode* root) { |
| 173 | + queue<TreeNode*> q{{root}}; |
| 174 | + int ans = 0; |
| 175 | + long long s = 1LL << 60; |
| 176 | + for (int level = 1; q.size(); ++level) { |
| 177 | + long long t = 0; |
| 178 | + for (int m = q.size(); m; --m) { |
| 179 | + TreeNode* node = q.front(); |
| 180 | + q.pop(); |
| 181 | + t += node->val; |
| 182 | + if (node->left) { |
| 183 | + q.push(node->left); |
| 184 | + } |
| 185 | + if (node->right) { |
| 186 | + q.push(node->right); |
| 187 | + } |
| 188 | + } |
| 189 | + if (s > t) { |
| 190 | + s = t; |
| 191 | + ans = level; |
| 192 | + } |
| 193 | + } |
| 194 | + return ans; |
| 195 | + } |
| 196 | +}; |
| 197 | +``` |
| 198 | +
|
| 199 | +#### Go |
| 200 | +
|
| 201 | +```go |
| 202 | +/** |
| 203 | + * Definition for a binary tree node. |
| 204 | + * type TreeNode struct { |
| 205 | + * Val int |
| 206 | + * Left *TreeNode |
| 207 | + * Right *TreeNode |
| 208 | + * } |
| 209 | + */ |
| 210 | +func minimumLevel(root *TreeNode) (ans int) { |
| 211 | + q := []*TreeNode{root} |
| 212 | + s := math.MaxInt64 |
| 213 | + for level := 1; len(q) > 0; level++ { |
| 214 | + t := 0 |
| 215 | + for m := len(q); m > 0; m-- { |
| 216 | + node := q[0] |
| 217 | + q = q[1:] |
| 218 | + t += node.Val |
| 219 | + if node.Left != nil { |
| 220 | + q = append(q, node.Left) |
| 221 | + } |
| 222 | + if node.Right != nil { |
| 223 | + q = append(q, node.Right) |
| 224 | + } |
| 225 | + } |
| 226 | + if s > t { |
| 227 | + s = t |
| 228 | + ans = level |
| 229 | + } |
| 230 | + } |
| 231 | + return |
| 232 | +} |
| 233 | +``` |
| 234 | + |
| 235 | +#### TypeScript |
| 236 | + |
| 237 | +```ts |
| 238 | +/** |
| 239 | + * Definition for a binary tree node. |
| 240 | + * class TreeNode { |
| 241 | + * val: number |
| 242 | + * left: TreeNode | null |
| 243 | + * right: TreeNode | null |
| 244 | + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { |
| 245 | + * this.val = (val===undefined ? 0 : val) |
| 246 | + * this.left = (left===undefined ? null : left) |
| 247 | + * this.right = (right===undefined ? null : right) |
| 248 | + * } |
| 249 | + * } |
| 250 | + */ |
| 251 | + |
| 252 | +function minimumLevel(root: TreeNode | null): number { |
| 253 | + const q: TreeNode[] = [root]; |
| 254 | + let s = Infinity; |
| 255 | + let ans = 0; |
| 256 | + for (let level = 1; q.length; ++level) { |
| 257 | + const qq: TreeNode[] = []; |
| 258 | + let t = 0; |
| 259 | + for (const { val, left, right } of q) { |
| 260 | + t += val; |
| 261 | + left && qq.push(left); |
| 262 | + right && qq.push(right); |
| 263 | + } |
| 264 | + if (s > t) { |
| 265 | + s = t; |
| 266 | + ans = level; |
| 267 | + } |
| 268 | + q.splice(0, q.length, ...qq); |
| 269 | + } |
| 270 | + return ans; |
| 271 | +} |
| 272 | +``` |
| 273 | + |
| 274 | +<!-- tabs:end --> |
| 275 | + |
| 276 | +<!-- solution:end --> |
| 277 | + |
| 278 | +<!-- problem:end --> |
0 commit comments