-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlib.rs
85 lines (83 loc) · 2.2 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* @lc app=leetcode.cn id=687 lang=rust
*
* [687] 最长同值路径
*/
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
struct Solution {}
// @lc code=start
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::cell::RefCell;
use std::cmp::max;
use std::rc::Rc;
impl Solution {
pub fn longest_univalue_path(mut root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
if root.is_none() {
return 0;
}
let res = &mut 0;
Self::arrow_length(&root, res);
*res
}
fn arrow_length(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut i32) -> i32 {
if root.is_none() {
return 0;
}
let root_borrow_mut = root.as_ref().unwrap().borrow();
let left = &root_borrow_mut.left;
let right = &root_borrow_mut.right;
let root_val = root_borrow_mut.val;
let arrow_length_left = Self::arrow_length(left, res);
let arrow_length_right = Self::arrow_length(right, res);
let mut left_length = 0;
let mut right_length = 0;
if left.is_some() {
let left_val = left.as_ref().unwrap().borrow().val;
if left_val == root_val {
left_length = arrow_length_left + 1;
}
}
if right.is_some() {
let right_val = right.as_ref().unwrap().borrow().val;
if right_val == root_val {
right_length = arrow_length_right + 1;
}
}
*res = max(*res, right_length + left_length);
return max(right_length, left_length);
}
}
// @lc code=end