diff --git a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/README.md b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/README.md index 412db8b003463..c39e1d71f15a8 100644 --- a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/README.md +++ b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/README.md @@ -58,7 +58,9 @@ tags: ### 方法一:BFS -BFS 找每一层最大的节点值。 +我们定义一个队列 $q$,将根节点放入队列中。每次从队列中取出当前层的所有节点,找出最大值,然后将下一层的所有节点放入队列中,直到队列为空。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。 @@ -73,20 +75,20 @@ BFS 找每一层最大的节点值。 # self.right = right class Solution: def largestValues(self, root: Optional[TreeNode]) -> List[int]: + ans = [] if root is None: - return [] + return ans q = deque([root]) - ans = [] while q: - t = -inf + x = -inf for _ in range(len(q)): node = q.popleft() - t = max(t, node.val) + x = max(x, node.val) if node.left: q.append(node.left) if node.right: q.append(node.right) - ans.append(t) + ans.append(x) return ans ``` @@ -152,19 +154,25 @@ class Solution { class Solution { public: vector largestValues(TreeNode* root) { - if (!root) return {}; - queue q{{root}}; vector ans; - while (!q.empty()) { - int t = q.front()->val; + if (!root) { + return ans; + } + queue q{{root}}; + while (q.size()) { + int x = INT_MIN; for (int i = q.size(); i; --i) { TreeNode* node = q.front(); - t = max(t, node->val); q.pop(); - if (node->left) q.push(node->left); - if (node->right) q.push(node->right); + x = max(x, node->val); + if (node->left) { + q.push(node->left); + } + if (node->right) { + q.push(node->right); + } } - ans.push_back(t); + ans.push_back(x); } return ans; } @@ -182,18 +190,17 @@ public: * Right *TreeNode * } */ -func largestValues(root *TreeNode) []int { - var ans []int +func largestValues(root *TreeNode) (ans []int) { if root == nil { - return ans + return } q := []*TreeNode{root} for len(q) > 0 { - t := q[0].Val + x := q[0].Val for i := len(q); i > 0; i-- { node := q[0] q = q[1:] - t = max(t, node.Val) + x = max(x, node.Val) if node.Left != nil { q = append(q, node.Left) } @@ -201,9 +208,9 @@ func largestValues(root *TreeNode) []int { q = append(q, node.Right) } } - ans = append(ans, t) + ans = append(ans, x) } - return ans + return } ``` @@ -225,23 +232,28 @@ func largestValues(root *TreeNode) []int { */ function largestValues(root: TreeNode | null): number[] { - const res: number[] = []; - const queue: TreeNode[] = []; - if (root) { - queue.push(root); + const ans: number[] = []; + if (!root) { + return ans; } - while (queue.length) { - const n = queue.length; - let max = -Infinity; - for (let i = 0; i < n; i++) { - const { val, left, right } = queue.shift(); - max = Math.max(max, val); - left && queue.push(left); - right && queue.push(right); + const q: TreeNode[] = [root]; + while (q.length) { + const nq: TreeNode[] = []; + let x = -Infinity; + for (const { val, left, right } of q) { + x = Math.max(x, val); + if (left) { + nq.push(left); + } + if (right) { + nq.push(right); + } } - res.push(max); + ans.push(x); + q.length = 0; + q.push(...nq); } - return res; + return ans; } ``` @@ -271,27 +283,27 @@ use std::collections::VecDeque; use std::rc::Rc; impl Solution { pub fn largest_values(root: Option>>) -> Vec { - let mut res = Vec::new(); - let mut queue = VecDeque::new(); + let mut ans = Vec::new(); + let mut q = VecDeque::new(); if root.is_some() { - queue.push_back(root.clone()); + q.push_back(root.clone()); } - while !queue.is_empty() { - let mut max = i32::MIN; - for _ in 0..queue.len() { - let node = queue.pop_front().unwrap(); + while !q.is_empty() { + let mut x = i32::MIN; + for _ in 0..q.len() { + let node = q.pop_front().unwrap(); let node = node.as_ref().unwrap().borrow(); - max = max.max(node.val); + x = x.max(node.val); if node.left.is_some() { - queue.push_back(node.left.clone()); + q.push_back(node.left.clone()); } if node.right.is_some() { - queue.push_back(node.right.clone()); + q.push_back(node.right.clone()); } } - res.push(max); + ans.push(x); } - res + ans } } ``` diff --git a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/README_EN.md b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/README_EN.md index f110ca8e293c3..86f545fe2f814 100644 --- a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/README_EN.md +++ b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/README_EN.md @@ -50,7 +50,11 @@ tags: -### Solution 1 +### Solution 1: BFS + +We define a queue $q$ and put the root node into the queue. Each time, we take out all the nodes of the current level from the queue, find the maximum value, and then put all the nodes of the next level into the queue until the queue is empty. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree. @@ -65,20 +69,20 @@ tags: # self.right = right class Solution: def largestValues(self, root: Optional[TreeNode]) -> List[int]: + ans = [] if root is None: - return [] + return ans q = deque([root]) - ans = [] while q: - t = -inf + x = -inf for _ in range(len(q)): node = q.popleft() - t = max(t, node.val) + x = max(x, node.val) if node.left: q.append(node.left) if node.right: q.append(node.right) - ans.append(t) + ans.append(x) return ans ``` @@ -144,19 +148,25 @@ class Solution { class Solution { public: vector largestValues(TreeNode* root) { - if (!root) return {}; - queue q{{root}}; vector ans; - while (!q.empty()) { - int t = q.front()->val; + if (!root) { + return ans; + } + queue q{{root}}; + while (q.size()) { + int x = INT_MIN; for (int i = q.size(); i; --i) { TreeNode* node = q.front(); - t = max(t, node->val); q.pop(); - if (node->left) q.push(node->left); - if (node->right) q.push(node->right); + x = max(x, node->val); + if (node->left) { + q.push(node->left); + } + if (node->right) { + q.push(node->right); + } } - ans.push_back(t); + ans.push_back(x); } return ans; } @@ -174,18 +184,17 @@ public: * Right *TreeNode * } */ -func largestValues(root *TreeNode) []int { - var ans []int +func largestValues(root *TreeNode) (ans []int) { if root == nil { - return ans + return } q := []*TreeNode{root} for len(q) > 0 { - t := q[0].Val + x := q[0].Val for i := len(q); i > 0; i-- { node := q[0] q = q[1:] - t = max(t, node.Val) + x = max(x, node.Val) if node.Left != nil { q = append(q, node.Left) } @@ -193,9 +202,9 @@ func largestValues(root *TreeNode) []int { q = append(q, node.Right) } } - ans = append(ans, t) + ans = append(ans, x) } - return ans + return } ``` @@ -217,23 +226,28 @@ func largestValues(root *TreeNode) []int { */ function largestValues(root: TreeNode | null): number[] { - const res: number[] = []; - const queue: TreeNode[] = []; - if (root) { - queue.push(root); + const ans: number[] = []; + if (!root) { + return ans; } - while (queue.length) { - const n = queue.length; - let max = -Infinity; - for (let i = 0; i < n; i++) { - const { val, left, right } = queue.shift(); - max = Math.max(max, val); - left && queue.push(left); - right && queue.push(right); + const q: TreeNode[] = [root]; + while (q.length) { + const nq: TreeNode[] = []; + let x = -Infinity; + for (const { val, left, right } of q) { + x = Math.max(x, val); + if (left) { + nq.push(left); + } + if (right) { + nq.push(right); + } } - res.push(max); + ans.push(x); + q.length = 0; + q.push(...nq); } - return res; + return ans; } ``` @@ -263,27 +277,27 @@ use std::collections::VecDeque; use std::rc::Rc; impl Solution { pub fn largest_values(root: Option>>) -> Vec { - let mut res = Vec::new(); - let mut queue = VecDeque::new(); + let mut ans = Vec::new(); + let mut q = VecDeque::new(); if root.is_some() { - queue.push_back(root.clone()); + q.push_back(root.clone()); } - while !queue.is_empty() { - let mut max = i32::MIN; - for _ in 0..queue.len() { - let node = queue.pop_front().unwrap(); + while !q.is_empty() { + let mut x = i32::MIN; + for _ in 0..q.len() { + let node = q.pop_front().unwrap(); let node = node.as_ref().unwrap().borrow(); - max = max.max(node.val); + x = x.max(node.val); if node.left.is_some() { - queue.push_back(node.left.clone()); + q.push_back(node.left.clone()); } if node.right.is_some() { - queue.push_back(node.right.clone()); + q.push_back(node.right.clone()); } } - res.push(max); + ans.push(x); } - res + ans } } ``` diff --git a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.cpp b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.cpp index 0c322d2bb8cc3..29ad429ad87f4 100644 --- a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.cpp +++ b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.cpp @@ -12,20 +12,26 @@ class Solution { public: vector largestValues(TreeNode* root) { - if (!root) return {}; - queue q{{root}}; vector ans; - while (!q.empty()) { - int t = q.front()->val; + if (!root) { + return ans; + } + queue q{{root}}; + while (q.size()) { + int x = INT_MIN; for (int i = q.size(); i; --i) { TreeNode* node = q.front(); - t = max(t, node->val); q.pop(); - if (node->left) q.push(node->left); - if (node->right) q.push(node->right); + x = max(x, node->val); + if (node->left) { + q.push(node->left); + } + if (node->right) { + q.push(node->right); + } } - ans.push_back(t); + ans.push_back(x); } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.go b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.go index 6aca3500b9db5..721d01c643450 100644 --- a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.go +++ b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.go @@ -6,18 +6,17 @@ * Right *TreeNode * } */ -func largestValues(root *TreeNode) []int { - var ans []int +func largestValues(root *TreeNode) (ans []int) { if root == nil { - return ans + return } q := []*TreeNode{root} for len(q) > 0 { - t := q[0].Val + x := q[0].Val for i := len(q); i > 0; i-- { node := q[0] q = q[1:] - t = max(t, node.Val) + x = max(x, node.Val) if node.Left != nil { q = append(q, node.Left) } @@ -25,7 +24,7 @@ func largestValues(root *TreeNode) []int { q = append(q, node.Right) } } - ans = append(ans, t) + ans = append(ans, x) } - return ans -} \ No newline at end of file + return +} diff --git a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.py b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.py index 05e9740d2dfd4..f7eb20e3a7026 100644 --- a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.py +++ b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.py @@ -6,18 +6,18 @@ # self.right = right class Solution: def largestValues(self, root: Optional[TreeNode]) -> List[int]: + ans = [] if root is None: - return [] + return ans q = deque([root]) - ans = [] while q: - t = -inf + x = -inf for _ in range(len(q)): node = q.popleft() - t = max(t, node.val) + x = max(x, node.val) if node.left: q.append(node.left) if node.right: q.append(node.right) - ans.append(t) + ans.append(x) return ans diff --git a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.rs b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.rs index 7f342dfbf69fb..395f7e6a75372 100644 --- a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.rs +++ b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.rs @@ -21,26 +21,26 @@ use std::collections::VecDeque; use std::rc::Rc; impl Solution { pub fn largest_values(root: Option>>) -> Vec { - let mut res = Vec::new(); - let mut queue = VecDeque::new(); + let mut ans = Vec::new(); + let mut q = VecDeque::new(); if root.is_some() { - queue.push_back(root.clone()); + q.push_back(root.clone()); } - while !queue.is_empty() { - let mut max = i32::MIN; - for _ in 0..queue.len() { - let node = queue.pop_front().unwrap(); + while !q.is_empty() { + let mut x = i32::MIN; + for _ in 0..q.len() { + let node = q.pop_front().unwrap(); let node = node.as_ref().unwrap().borrow(); - max = max.max(node.val); + x = x.max(node.val); if node.left.is_some() { - queue.push_back(node.left.clone()); + q.push_back(node.left.clone()); } if node.right.is_some() { - queue.push_back(node.right.clone()); + q.push_back(node.right.clone()); } } - res.push(max); + ans.push(x); } - res + ans } } diff --git a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.ts b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.ts index 383ad0b50b6b9..150a68ff06ed8 100644 --- a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.ts +++ b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.ts @@ -13,21 +13,26 @@ */ function largestValues(root: TreeNode | null): number[] { - const res: number[] = []; - const queue: TreeNode[] = []; - if (root) { - queue.push(root); + const ans: number[] = []; + if (!root) { + return ans; } - while (queue.length) { - const n = queue.length; - let max = -Infinity; - for (let i = 0; i < n; i++) { - const { val, left, right } = queue.shift(); - max = Math.max(max, val); - left && queue.push(left); - right && queue.push(right); + const q: TreeNode[] = [root]; + while (q.length) { + const nq: TreeNode[] = []; + let x = -Infinity; + for (const { val, left, right } of q) { + x = Math.max(x, val); + if (left) { + nq.push(left); + } + if (right) { + nq.push(right); + } } - res.push(max); + ans.push(x); + q.length = 0; + q.push(...nq); } - return res; + return ans; }