Skip to content

Commit 0f5475a

Browse files
committed
add 129; 144; 148
1 parent d7a1fad commit 0f5475a

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

129.sum-root-to-leaf-numbers.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @lc app=leetcode id=129 lang=rust
3+
*
4+
* [129] Sum Root to Leaf Numbers
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn sum_numbers(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
30+
fn helper(root: &Option<Rc<RefCell<TreeNode>>>, presum: i32) -> i32 {
31+
match root {
32+
Some(rc_node) => {
33+
let tree_node = rc_node.borrow();
34+
if tree_node.left.is_none() && tree_node.right.is_none() {
35+
presum * 10 + tree_node.val
36+
} else {
37+
helper(&tree_node.left, presum * 10 + tree_node.val) +
38+
helper(&tree_node.right, presum * 10 + tree_node.val)
39+
}
40+
},
41+
None => 0
42+
}
43+
}
44+
45+
helper(&root, 0)
46+
}
47+
}
48+
// @lc code=end
49+

144.binary-tree-preorder-traversal.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @lc app=leetcode id=144 lang=rust
3+
*
4+
* [144] Binary Tree Preorder Traversal
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
30+
let mut ans = vec![];
31+
32+
fn preorder(root: Option<Rc<RefCell<TreeNode>>>, vals: &mut Vec<i32>) {
33+
match root {
34+
Some(rc_node) => {
35+
let tree_node = rc_node.borrow();
36+
vals.push(tree_node.val);
37+
preorder(tree_node.left.clone(), vals);
38+
preorder(tree_node.right.clone(), vals);
39+
},
40+
None => ()
41+
}
42+
}
43+
44+
preorder(root, &mut ans);
45+
ans
46+
}
47+
}
48+
// @lc code=end
49+

148.sort-list.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* @lc app=leetcode id=148 lang=rust
3+
*
4+
* [148] Sort List
5+
*/
6+
7+
// @lc code=start
8+
// Definition for singly-linked list.
9+
// #[derive(PartialEq, Eq, Clone, Debug)]
10+
// pub struct ListNode {
11+
// pub val: i32,
12+
// pub next: Option<Box<ListNode>>
13+
// }
14+
//
15+
// impl ListNode {
16+
// #[inline]
17+
// fn new(val: i32) -> Self {
18+
// ListNode {
19+
// next: None,
20+
// val
21+
// }
22+
// }
23+
// }
24+
impl Solution {
25+
pub fn sort_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
26+
let N = Self::length(&head);
27+
Self::sort_helper(head, N)
28+
}
29+
30+
pub fn sort_helper(mut head: Option<Box<ListNode>>, len: u32) -> Option<Box<ListNode>> {
31+
if len <= 1 { return head; }
32+
let mid = len / 2;
33+
34+
let mut hmut = head.as_mut();
35+
let mut i = 1;
36+
37+
while i < mid {
38+
hmut = hmut.unwrap().next.as_mut();
39+
i += 1;
40+
}
41+
42+
// println!("len: {}, mid: {}", len, mid);
43+
Self::merge(
44+
Self::sort_helper(hmut.unwrap().next.take(), len - mid),
45+
Self::sort_helper(head, mid)
46+
)
47+
}
48+
49+
pub fn length(head: &Option<Box<ListNode>>) -> u32 {
50+
let mut href = head.as_ref();
51+
let mut N = 0;
52+
53+
while let Some(node) = href {
54+
href = node.next.as_ref();
55+
N += 1;
56+
}
57+
58+
N
59+
}
60+
61+
pub fn merge(mut h1: Option<Box<ListNode>>, mut h2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
62+
match (h1.as_mut(), h2.as_mut()) {
63+
(Some(mut node1), Some(mut node2)) => {
64+
if node1.val < node2.val {
65+
node1.next = Self::merge(node1.next.take(), h2);
66+
h1
67+
} else {
68+
node2.next = Self::merge(h1, node2.next.take());
69+
h2
70+
}
71+
},
72+
(Some(node1), None) => h1,
73+
(None, Some(node2)) => h2,
74+
(None, None) => None
75+
}
76+
}
77+
}
78+
// @lc code=end
79+

0 commit comments

Comments
 (0)