|
| 1 | +/** |
| 2 | + * [897] Increasing Order Search Tree |
| 3 | + * |
| 4 | + * Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child. |
| 5 | + * |
| 6 | + * |
| 7 | + * Example 1: |
| 8 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/ex1.jpg" style="width: 600px; height: 350px;" /> |
| 9 | + * |
| 10 | + * Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9] |
| 11 | + * Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9] |
| 12 | + * |
| 13 | + * |
| 14 | + * Example 2: |
| 15 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/ex2.jpg" style="width: 300px; height: 114px;" /> |
| 16 | + * |
| 17 | + * Input: root = [5,1,7] |
| 18 | + * Output: [1,null,5,null,7] |
| 19 | + * |
| 20 | + * |
| 21 | + * |
| 22 | + * Constraints: |
| 23 | + * |
| 24 | + * |
| 25 | + * The number of nodes in the given tree will be in the range [1, 100]. |
| 26 | + * 0 <= Node.val <= 1000 |
| 27 | + * |
| 28 | + */ |
| 29 | +pub struct Solution {} |
| 30 | +use crate::util::tree::{TreeNode, to_tree}; |
| 31 | + |
| 32 | +// problem: https://leetcode.com/problems/increasing-order-search-tree/ |
| 33 | +// discuss: https://leetcode.com/problems/increasing-order-search-tree/discuss/?currentPage=1&orderBy=most_votes&query= |
| 34 | + |
| 35 | +// submission codes start here |
| 36 | + |
| 37 | +// Definition for a binary tree node. |
| 38 | +// #[derive(Debug, PartialEq, Eq)] |
| 39 | +// pub struct TreeNode { |
| 40 | +// pub val: i32, |
| 41 | +// pub left: Option<Rc<RefCell<TreeNode>>>, |
| 42 | +// pub right: Option<Rc<RefCell<TreeNode>>>, |
| 43 | +// } |
| 44 | +// |
| 45 | +// impl TreeNode { |
| 46 | +// #[inline] |
| 47 | +// pub fn new(val: i32) -> Self { |
| 48 | +// TreeNode { |
| 49 | +// val, |
| 50 | +// left: None, |
| 51 | +// right: None |
| 52 | +// } |
| 53 | +// } |
| 54 | +// } |
| 55 | +use std::{borrow::Borrow, rc::Rc}; |
| 56 | +use std::cell::RefCell; |
| 57 | +impl Solution { |
| 58 | + pub fn increasing_bst(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> { |
| 59 | + // travel the root, get the list with increasing order |
| 60 | + let mut dummy_node = Rc::new(RefCell::new(TreeNode::new(0))); |
| 61 | + let mut current_node: Rc<RefCell<TreeNode>> = dummy_node.clone(); |
| 62 | + let mut node_list = vec![]; |
| 63 | + Self::in_order(root, &mut node_list); |
| 64 | + |
| 65 | + for node in node_list { |
| 66 | + current_node.borrow_mut().right = Some(node.clone()); |
| 67 | + current_node = node; |
| 68 | + } |
| 69 | + let res = dummy_node.borrow_mut().right.take(); |
| 70 | + return res; |
| 71 | + } |
| 72 | + |
| 73 | + pub fn in_order(node: Option<Rc<RefCell<TreeNode>>>, node_list: &mut Vec<Rc<RefCell<TreeNode>>>) { |
| 74 | + if let Some(node) = node { |
| 75 | + let lc = node.borrow_mut().left.take(); |
| 76 | + let rc = node.borrow_mut().right.take(); |
| 77 | + Self::in_order(lc, node_list); |
| 78 | + node_list.push(node); |
| 79 | + Self::in_order(rc, node_list); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/* |
| 85 | +impl Solution { |
| 86 | + pub fn increasing_bst(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> { |
| 87 | + // Passed 0ms 1.9mb |
| 88 | + fn in_order(node: Option<Rc<RefCell<TreeNode>>>, arr: &mut Vec<Rc<RefCell<TreeNode>>>) { |
| 89 | + if let Some(node) = node { |
| 90 | + let left = node.borrow_mut().left.take(); |
| 91 | + let right = node.borrow_mut().right.take(); |
| 92 | + in_order(left, arr); |
| 93 | + arr.push(node); |
| 94 | + in_order(right, arr); |
| 95 | + } |
| 96 | + } |
| 97 | + let mut arr = Vec::new(); |
| 98 | + in_order(root, &mut arr); |
| 99 | + let mut dummy = Rc::new(RefCell::new(TreeNode { val: 0, left: None, right: None })); |
| 100 | + let mut cur = dummy.clone(); |
| 101 | + arr.into_iter().for_each(|node| { |
| 102 | + cur.borrow_mut().right = Some(node.clone()); |
| 103 | + cur = node; |
| 104 | + }); |
| 105 | + let root = dummy.borrow_mut().right.take(); |
| 106 | + root |
| 107 | + } |
| 108 | +} |
| 109 | +*/ |
| 110 | + |
| 111 | +// submission codes end |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use super::*; |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_897() { |
| 119 | + let root = tree![5,3,6,2,4,null,8,1,null,null,null,7,9]; |
| 120 | + let res = tree![1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]; |
| 121 | + |
| 122 | + assert_eq!(Solution::increasing_bst(root), res); |
| 123 | + } |
| 124 | +} |
0 commit comments