Skip to content

Commit b00e170

Browse files
committed
'Flatten Binary Tree to Linked List' soln
1 parent a6a1bff commit b00e170

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ Note: Some solutions have multiple approaches implemented
119119
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Solution](./leetcode/minimum_depth_of_binary_tree.rs) | Easy |
120120
| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Solution](./leetcode/path_sum.rs) | Easy |
121121
| 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [Solution](./leetcode/path_sum_ii.rs) | Medium |
122-
| >114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [Solution](./leetcode/flatten_binary_tree.rs) | Medium |
123-
| 115 | [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/) | [Solution](./leetcode/distinct_subsequences.rs) | Hard |
122+
| 114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [Solution](./leetcode/flatten_binary_tree.rs) | Medium |
123+
| >115 | [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/) | [Solution](./leetcode/distinct_subsequences.rs) | Hard |
124124
| 116 | [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/) | [Solution](./leetcode/populating_next_right_pointers.rs) | Medium |
125125
| 117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/) | [Solution](./leetcode/populating_next_right_pointers_ii.rs) | Medium |
126126
| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | [Solution](./leetcode/pascals_triangle.rs) | Easy |

leetcode/flatten_binary_tree.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
///
2+
/// Problem: Flatten Binary Tree to Linked List
3+
///
4+
/// Given the root of a binary tree, flatten the tree into a "linked list":
5+
///
6+
/// The "linked list" should use the same TreeNode class where the right child pointer points
7+
/// to the next node in the list and the left child pointer is always null.
8+
///
9+
/// The "linked list" should be in the same order as a pre-order traversal of the binary tree.
10+
///
11+
/// Example 1:
12+
/// Input: root = [1,2,5,3,4,null,6]
13+
/// Output: [1,null,2,null,3,null,4,null,5,null,6]
14+
///
15+
/// Example 2:
16+
/// Input: root = []
17+
/// Output: []
18+
///
19+
/// Example 3:
20+
/// Input: root = [0]
21+
/// Output: [0]
22+
///
23+
/// Constraints:
24+
/// The number of nodes in the tree is in the range [0, 2000].
25+
/// -100 <= Node.val <= 100
26+
///
27+
/// Follow up: Can you flatten the tree in-place (with O(1) extra space)?
28+
///
29+
30+
// Definition for a binary tree node (provided by LeetCode)
31+
// #[derive(Debug, PartialEq, Eq)]
32+
// pub struct TreeNode {
33+
// pub val: i32,
34+
// pub left: Option<Rc<RefCell<TreeNode>>>,
35+
// pub right: Option<Rc<RefCell<TreeNode>>>,
36+
// }
37+
//
38+
// impl TreeNode {
39+
// #[inline]
40+
// pub fn new(val: i32) -> Self {
41+
// TreeNode {
42+
// val,
43+
// left: None,
44+
// right: None
45+
// }
46+
// }
47+
// }
48+
49+
// # Solution
50+
// Time complexity: O(n)
51+
// Space complexity: O(h) - Where h is the height of the tree
52+
53+
54+
55+
56+
use std::rc::Rc;
57+
use std::cell::RefCell;
58+
impl Solution {
59+
pub fn flatten(root: &mut Option<Rc<RefCell<TreeNode>>>) {
60+
let mut nodes = Vec::new();
61+
Self::preorder_traversal(root.clone(), &mut nodes);
62+
63+
// Flatten the tree
64+
if let Some(current_node) = root {
65+
let mut current_ref = current_node.borrow_mut();
66+
67+
// Start with the first node
68+
for i in 1..nodes.len() {
69+
// Set right child to the next node in preorder
70+
current_ref.right = Some(Rc::clone(&nodes[i]));
71+
current_ref.left = None;
72+
73+
// Move to the next node
74+
current_ref = nodes[i].borrow_mut();
75+
}
76+
77+
// Set the leaf node's pointers
78+
current_ref.left = None;
79+
current_ref.right = None;
80+
}
81+
}
82+
83+
fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>, nodes: &mut Vec<Rc<RefCell<TreeNode>>>) {
84+
if let Some(node) = root {
85+
nodes.push(Rc::clone(&node));
86+
87+
let node_ref = node.borrow();
88+
Self::preorder_traversal(node_ref.left.clone(), nodes);
89+
Self::preorder_traversal(node_ref.right.clone(), nodes);
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)