114. Flatten Binary Tree to Linked List Medium
1/**
2 * [114] 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 to the next node in the list and the left child pointer is always null.
7 * The "linked list" should be in the same order as a <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR" target="_blank">pre-order traversal</a> of the binary tree.
8 *
9 *
10 * Example 1:
11 * <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
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 *
17 * Input: root = []
18 * Output: []
19 *
20 * Example 3:
21 *
22 * Input: root = [0]
23 * Output: [0]
24 *
25 *
26 * Constraints:
27 *
28 * The number of nodes in the tree is in the range [0, 2000].
29 * -100 <= Node.val <= 100
30 *
31 *
32 * Follow up: Can you flatten the tree in-place (with O(1) extra space)?
33 */
34pub struct Solution {}
35use crate::util::tree::{TreeNode, to_tree};
36
37// problem: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
38// discuss: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42// Definition for a binary tree node.
43// #[derive(Debug, PartialEq, Eq)]
44// pub struct TreeNode {
45// pub val: i32,
46// pub left: Option<Rc<RefCell<TreeNode>>>,
47// pub right: Option<Rc<RefCell<TreeNode>>>,
48// }
49//
50// impl TreeNode {
51// #[inline]
52// pub fn new(val: i32) -> Self {
53// TreeNode {
54// val,
55// left: None,
56// right: None
57// }
58// }
59// }
60use std::rc::Rc;
61use std::cell::RefCell;
62impl Solution {
63 pub fn flatten(root: &mut Option<Rc<RefCell<TreeNode>>>) {
64
65 }
66}
67
68// submission codes end
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn test_114() {
76 }
77}
78
Back
© 2025 bowen.ge All Rights Reserved.