comments | difficulty | edit_url | tags | ||||
---|---|---|---|---|---|---|---|
true |
中等 |
|
给定一棵二叉树的根 root
,其中每个节点有一个值,返回树中 层和最小 的层数(如果相等,返回 最低 的层数)。
注意 树的根节点在第一层,其它任何节点的层数是它到根节点的距离+1。
示例 1:
示例 2:
示例 3:
提示:
- 树中节点数量的范围是
[1, 105]
。 1 <= Node.val <= 109
我们可以使用 BFS,逐层遍历二叉树,记录每一层的节点值之和,找到具有最小节点值之和的层,返回该层的层数。
时间复杂度
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minimumLevel(self, root: Optional[TreeNode]) -> int:
q = deque([root])
ans = 0
level, s = 1, inf
while q:
t = 0
for _ in range(len(q)):
node = q.popleft()
t += node.val
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
if s > t:
s = t
ans = level
level += 1
return ans
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minimumLevel(TreeNode root) {
Deque<TreeNode> q = new ArrayDeque<>();
q.offer(root);
int ans = 0;
long s = Long.MAX_VALUE;
for (int level = 1; !q.isEmpty(); ++level) {
long t = 0;
for (int m = q.size(); m > 0; --m) {
TreeNode node = q.poll();
t += node.val;
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
}
if (s > t) {
s = t;
ans = level;
}
}
return ans;
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minimumLevel(TreeNode* root) {
queue<TreeNode*> q{{root}};
int ans = 0;
long long s = 1LL << 60;
for (int level = 1; q.size(); ++level) {
long long t = 0;
for (int m = q.size(); m; --m) {
TreeNode* node = q.front();
q.pop();
t += node->val;
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
if (s > t) {
s = t;
ans = level;
}
}
return ans;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minimumLevel(root *TreeNode) (ans int) {
q := []*TreeNode{root}
s := math.MaxInt64
for level := 1; len(q) > 0; level++ {
t := 0
for m := len(q); m > 0; m-- {
node := q[0]
q = q[1:]
t += node.Val
if node.Left != nil {
q = append(q, node.Left)
}
if node.Right != nil {
q = append(q, node.Right)
}
}
if s > t {
s = t
ans = level
}
}
return
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function minimumLevel(root: TreeNode | null): number {
const q: TreeNode[] = [root];
let s = Infinity;
let ans = 0;
for (let level = 1; q.length; ++level) {
const qq: TreeNode[] = [];
let t = 0;
for (const { val, left, right } of q) {
t += val;
left && qq.push(left);
right && qq.push(right);
}
if (s > t) {
s = t;
ans = level;
}
q.splice(0, q.length, ...qq);
}
return ans;
}