From d1670387448ca7f09340b717e91157a8697cb3e7 Mon Sep 17 00:00:00 2001 From: li-chx Date: Thu, 13 Aug 2026 19:21:39 +0800 Subject: [PATCH] =?UTF-8?q?199.=20=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E5=8F=B3=E8=A7=86=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index b7fbd63..f40fdbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,28 +3,29 @@ mod tree; use crate::tree::{TreeNode, build_tree_from_str}; struct Solution {} -use std::cell::RefCell; use std::rc::Rc; +use std::cell::RefCell; impl Solution { - pub fn max_path_sum(root: Option>>) -> i32 { - let (_,ans) = Self::dfs(&root); - ans + pub fn right_side_view(root: Option>>) -> Vec { + let mut result = vec![]; + Self::dfs(root, &mut result,0); + result } - fn dfs(root: &Option>>) -> (i32, i32) { + fn dfs(root: Option>>, mut arr: &mut Vec,depth: usize) { if root.is_none() { - return (-100000000, -100000000); + return; } - let (la, ln) = Self::dfs(&root.as_ref().unwrap().borrow().left); - let (ra, rn) = Self::dfs(&root.as_ref().unwrap().borrow().right); - let val = root.as_ref().unwrap().borrow().val; - let a = val.max(val + la).max(val + ra); - let n = a.max(ln).max(rn).max(val + la + ra); - (a, n) + while arr.len() <= depth { + arr.push(0); + } + (*arr)[depth] = root.as_ref().unwrap().borrow().val; + Self::dfs(root.as_ref().unwrap().borrow().left.clone(), &mut arr,depth + 1); + Self::dfs(root.as_ref().unwrap().borrow().right.clone(), &mut arr,depth + 1); } } fn main() { println!( "{:?}", - Solution::max_path_sum(build_tree_from_str("[5,4,8,11,null,13,4,7,2,null,null,null,1]")) + Solution::right_side_view(build_tree_from_str("[1,2,3,4,null,null,null,5]")) ); }