LeetCode-in-All

114. Flatten Binary Tree to Linked List

Medium

Given the root of a binary tree, flatten the tree into a “linked list”:

Example 1:

Input: root = [1,2,5,3,4,null,6]

Output: [1,null,2,null,3,null,4,null,5,null,6]

Example 2:

Input: root = []

Output: []

Example 3:

Input: root = [0]

Output: [0]

Constraints:

Follow up: Can you flatten the tree in-place (with O(1) extra space)?

Solution

// Definition for a binary tree node.
// 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::rc::Rc;
use std::cell::RefCell;
type Node = Option<Rc<RefCell<TreeNode>>>;
impl Solution {
    pub fn build_tree(inorder: Vec<i32>, postorder: Vec<i32>) -> Node {
        Solution::builder(&inorder[..], &postorder[..])
    }
    fn builder(inorder: &[i32], postorder: &[i32]) -> Node { 
        let i = inorder.len();
        if inorder.is_empty() || postorder.is_empty() { 
            return None 
        } else { 
            let n = postorder.len() -1;
            let mut root = TreeNode::new(postorder[n]);
            let m = inorder.iter().position(|&x| x == postorder[n]).unwrap();
            root.left = Solution::builder(&inorder[0..m], &postorder[0..m]);
            root.right = Solution::builder(&inorder[m+1..i], &postorder[m..i-1]);
            Some(Rc::new(RefCell::new(root)))
        }
    }
}