Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cd8e915

Browse files
committed
feat: add solutions to lc problem: No.0515
No.0515.Find Largest Value in Each Tree Row
1 parent faf59bf commit cd8e915

File tree

4 files changed

+416
-1
lines changed

4 files changed

+416
-1
lines changed

‎solution/0500-0599/0515.Find Largest Value in Each Tree Row/README.md‎

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Solution:
9999
ans[curr] = max(ans[curr], root.val)
100100
dfs(root.left, curr + 1)
101101
dfs(root.right, curr + 1)
102-
102+
103103
ans = []
104104
dfs(root, 0)
105105
return ans
@@ -339,6 +339,174 @@ func max(a, b int) int {
339339
}
340340
```
341341

342+
### **TypeScript**
343+
344+
```ts
345+
/**
346+
* Definition for a binary tree node.
347+
* class TreeNode {
348+
* val: number
349+
* left: TreeNode | null
350+
* right: TreeNode | null
351+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
352+
* this.val = (val===undefined ? 0 : val)
353+
* this.left = (left===undefined ? null : left)
354+
* this.right = (right===undefined ? null : right)
355+
* }
356+
* }
357+
*/
358+
359+
function largestValues(root: TreeNode | null): number[] {
360+
const res: number[] = [];
361+
const queue: TreeNode[] = [];
362+
if (root) {
363+
queue.push(root);
364+
}
365+
while (queue.length) {
366+
const n = queue.length;
367+
let max = -Infinity;
368+
for (let i = 0; i < n; i++) {
369+
const { val, left, right } = queue.shift();
370+
max = Math.max(max, val);
371+
left && queue.push(left);
372+
right && queue.push(right);
373+
}
374+
res.push(max);
375+
}
376+
return res;
377+
}
378+
```
379+
380+
```ts
381+
/**
382+
* Definition for a binary tree node.
383+
* class TreeNode {
384+
* val: number
385+
* left: TreeNode | null
386+
* right: TreeNode | null
387+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
388+
* this.val = (val===undefined ? 0 : val)
389+
* this.left = (left===undefined ? null : left)
390+
* this.right = (right===undefined ? null : right)
391+
* }
392+
* }
393+
*/
394+
395+
function largestValues(root: TreeNode | null): number[] {
396+
const res = [];
397+
const dfs = (root: TreeNode | null, depth: number) => {
398+
if (root == null) {
399+
return;
400+
}
401+
const { val, left, right } = root;
402+
if (res.length == depth) {
403+
res.push(val);
404+
} else {
405+
res[depth] = Math.max(res[depth], val);
406+
}
407+
dfs(left, depth + 1);
408+
dfs(right, depth + 1);
409+
};
410+
dfs(root, 0);
411+
return res;
412+
}
413+
```
414+
415+
### **Rust**
416+
417+
```rust
418+
// Definition for a binary tree node.
419+
// #[derive(Debug, PartialEq, Eq)]
420+
// pub struct TreeNode {
421+
// pub val: i32,
422+
// pub left: Option<Rc<RefCell<TreeNode>>>,
423+
// pub right: Option<Rc<RefCell<TreeNode>>>,
424+
// }
425+
//
426+
// impl TreeNode {
427+
// #[inline]
428+
// pub fn new(val: i32) -> Self {
429+
// TreeNode {
430+
// val,
431+
// left: None,
432+
// right: None
433+
// }
434+
// }
435+
// }
436+
use std::rc::Rc;
437+
use std::cell::RefCell;
438+
use std::collections::VecDeque;
439+
impl Solution {
440+
pub fn largest_values(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
441+
let mut res = Vec::new();
442+
let mut queue = VecDeque::new();
443+
if root.is_some() {
444+
queue.push_back(root.clone());
445+
}
446+
while !queue.is_empty() {
447+
let mut max = i32::MIN;
448+
for _ in 0..queue.len() {
449+
let node = queue.pop_front().unwrap();
450+
let node = node.as_ref().unwrap().borrow();
451+
max = max.max(node.val);
452+
if node.left.is_some() {
453+
queue.push_back(node.left.clone());
454+
}
455+
if node.right.is_some() {
456+
queue.push_back(node.right.clone());
457+
}
458+
}
459+
res.push(max);
460+
}
461+
res
462+
}
463+
}
464+
```
465+
466+
```rust
467+
// Definition for a binary tree node.
468+
// #[derive(Debug, PartialEq, Eq)]
469+
// pub struct TreeNode {
470+
// pub val: i32,
471+
// pub left: Option<Rc<RefCell<TreeNode>>>,
472+
// pub right: Option<Rc<RefCell<TreeNode>>>,
473+
// }
474+
//
475+
// impl TreeNode {
476+
// #[inline]
477+
// pub fn new(val: i32) -> Self {
478+
// TreeNode {
479+
// val,
480+
// left: None,
481+
// right: None
482+
// }
483+
// }
484+
// }
485+
use std::rc::Rc;
486+
use std::cell::RefCell;
487+
impl Solution {
488+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, depth: usize, res: &mut Vec<i32>) {
489+
if root.is_none() {
490+
return;
491+
}
492+
let node = root.as_ref().unwrap().borrow();
493+
if res.len() == depth {
494+
res.push(node.val);
495+
} else {
496+
res[depth] = res[depth].max(node.val);
497+
}
498+
Self::dfs(&node.left, depth + 1, res);
499+
Self::dfs(&node.right, depth + 1, res);
500+
}
501+
502+
pub fn largest_values(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
503+
let mut res = Vec::new();
504+
Self::dfs(&root, 0, &mut res);
505+
res
506+
}
507+
}
508+
```
509+
342510
### **...**
343511

344512
```

‎solution/0500-0599/0515.Find Largest Value in Each Tree Row/README_EN.md‎

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,174 @@ func max(a, b int) int {
317317
}
318318
```
319319

320+
### **TypeScript**
321+
322+
```ts
323+
/**
324+
* Definition for a binary tree node.
325+
* class TreeNode {
326+
* val: number
327+
* left: TreeNode | null
328+
* right: TreeNode | null
329+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
330+
* this.val = (val===undefined ? 0 : val)
331+
* this.left = (left===undefined ? null : left)
332+
* this.right = (right===undefined ? null : right)
333+
* }
334+
* }
335+
*/
336+
337+
function largestValues(root: TreeNode | null): number[] {
338+
const res: number[] = [];
339+
const queue: TreeNode[] = [];
340+
if (root) {
341+
queue.push(root);
342+
}
343+
while (queue.length) {
344+
const n = queue.length;
345+
let max = -Infinity;
346+
for (let i = 0; i < n; i++) {
347+
const { val, left, right } = queue.shift();
348+
max = Math.max(max, val);
349+
left && queue.push(left);
350+
right && queue.push(right);
351+
}
352+
res.push(max);
353+
}
354+
return res;
355+
}
356+
```
357+
358+
```ts
359+
/**
360+
* Definition for a binary tree node.
361+
* class TreeNode {
362+
* val: number
363+
* left: TreeNode | null
364+
* right: TreeNode | null
365+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
366+
* this.val = (val===undefined ? 0 : val)
367+
* this.left = (left===undefined ? null : left)
368+
* this.right = (right===undefined ? null : right)
369+
* }
370+
* }
371+
*/
372+
373+
function largestValues(root: TreeNode | null): number[] {
374+
const res = [];
375+
const dfs = (root: TreeNode | null, depth: number) => {
376+
if (root == null) {
377+
return;
378+
}
379+
const { val, left, right } = root;
380+
if (res.length == depth) {
381+
res.push(val);
382+
} else {
383+
res[depth] = Math.max(res[depth], val);
384+
}
385+
dfs(left, depth + 1);
386+
dfs(right, depth + 1);
387+
};
388+
dfs(root, 0);
389+
return res;
390+
}
391+
```
392+
393+
### **Rust**
394+
395+
```rust
396+
// Definition for a binary tree node.
397+
// #[derive(Debug, PartialEq, Eq)]
398+
// pub struct TreeNode {
399+
// pub val: i32,
400+
// pub left: Option<Rc<RefCell<TreeNode>>>,
401+
// pub right: Option<Rc<RefCell<TreeNode>>>,
402+
// }
403+
//
404+
// impl TreeNode {
405+
// #[inline]
406+
// pub fn new(val: i32) -> Self {
407+
// TreeNode {
408+
// val,
409+
// left: None,
410+
// right: None
411+
// }
412+
// }
413+
// }
414+
use std::rc::Rc;
415+
use std::cell::RefCell;
416+
use std::collections::VecDeque;
417+
impl Solution {
418+
pub fn largest_values(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
419+
let mut res = Vec::new();
420+
let mut queue = VecDeque::new();
421+
if root.is_some() {
422+
queue.push_back(root.clone());
423+
}
424+
while !queue.is_empty() {
425+
let mut max = i32::MIN;
426+
for _ in 0..queue.len() {
427+
let node = queue.pop_front().unwrap();
428+
let node = node.as_ref().unwrap().borrow();
429+
max = max.max(node.val);
430+
if node.left.is_some() {
431+
queue.push_back(node.left.clone());
432+
}
433+
if node.right.is_some() {
434+
queue.push_back(node.right.clone());
435+
}
436+
}
437+
res.push(max);
438+
}
439+
res
440+
}
441+
}
442+
```
443+
444+
```rust
445+
// Definition for a binary tree node.
446+
// #[derive(Debug, PartialEq, Eq)]
447+
// pub struct TreeNode {
448+
// pub val: i32,
449+
// pub left: Option<Rc<RefCell<TreeNode>>>,
450+
// pub right: Option<Rc<RefCell<TreeNode>>>,
451+
// }
452+
//
453+
// impl TreeNode {
454+
// #[inline]
455+
// pub fn new(val: i32) -> Self {
456+
// TreeNode {
457+
// val,
458+
// left: None,
459+
// right: None
460+
// }
461+
// }
462+
// }
463+
use std::rc::Rc;
464+
use std::cell::RefCell;
465+
impl Solution {
466+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, depth: usize, res: &mut Vec<i32>) {
467+
if root.is_none() {
468+
return;
469+
}
470+
let node = root.as_ref().unwrap().borrow();
471+
if res.len() == depth {
472+
res.push(node.val);
473+
} else {
474+
res[depth] = res[depth].max(node.val);
475+
}
476+
Self::dfs(&node.left, depth + 1, res);
477+
Self::dfs(&node.right, depth + 1, res);
478+
}
479+
480+
pub fn largest_values(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
481+
let mut res = Vec::new();
482+
Self::dfs(&root, 0, &mut res);
483+
res
484+
}
485+
}
486+
```
487+
320488
### **...**
321489

322490
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /