|
| 1 | +/** |
| 2 | + * [99] Recover Binary Search Tree |
| 3 | + * |
| 4 | + * You are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure. |
| 5 | + * |
| 6 | + * <strong class="example">Example 1: |
| 7 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/28/recover1.jpg" style="width: 422px; height: 302px;" /> |
| 8 | + * Input: root = [1,3,null,null,2] |
| 9 | + * Output: [3,1,null,null,2] |
| 10 | + * Explanation: 3 cannot be a left child of 1 because 3 > 1. Swapping 1 and 3 makes the BST valid. |
| 11 | + * |
| 12 | + * <strong class="example">Example 2: |
| 13 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/28/recover2.jpg" style="width: 581px; height: 302px;" /> |
| 14 | + * Input: root = [3,1,4,null,null,2] |
| 15 | + * Output: [2,1,4,null,null,3] |
| 16 | + * Explanation: 2 cannot be in the right subtree of 3 because 2 < 3. Swapping 2 and 3 makes the BST valid. |
| 17 | + * |
| 18 | + * |
| 19 | + * Constraints: |
| 20 | + * |
| 21 | + * The number of nodes in the tree is in the range [2, 1000]. |
| 22 | + * -2^31 <= Node.val <= 2^31 - 1 |
| 23 | + * |
| 24 | + * |
| 25 | + * Follow up: A solution using O(n) space is pretty straight-forward. Could you devise a constant O(1) space solution? |
| 26 | + */ |
| 27 | +pub struct Solution {} |
| 28 | +use crate::util::tree::{TreeNode, to_tree}; |
| 29 | + |
| 30 | +// problem: https://leetcode.com/problems/recover-binary-search-tree/ |
| 31 | +// discuss: https://leetcode.com/problems/recover-binary-search-tree/discuss/?currentPage=1&orderBy=most_votes&query= |
| 32 | + |
| 33 | +// submission codes start here |
| 34 | + |
| 35 | +// Definition for a binary tree node. |
| 36 | +// #[derive(Debug, PartialEq, Eq)] |
| 37 | +// pub struct TreeNode { |
| 38 | +// pub val: i32, |
| 39 | +// pub left: Option<Rc<RefCell<TreeNode>>>, |
| 40 | +// pub right: Option<Rc<RefCell<TreeNode>>>, |
| 41 | +// } |
| 42 | +// |
| 43 | +// impl TreeNode { |
| 44 | +// #[inline] |
| 45 | +// pub fn new(val: i32) -> Self { |
| 46 | +// TreeNode { |
| 47 | +// val, |
| 48 | +// left: None, |
| 49 | +// right: None |
| 50 | +// } |
| 51 | +// } |
| 52 | +// } |
| 53 | +use std::rc::Rc; |
| 54 | +use std::cell::RefCell; |
| 55 | +impl Solution { |
| 56 | + pub fn recover_tree(root: &mut Option<Rc<RefCell<TreeNode>>>) { |
| 57 | + let mut res = vec![]; |
| 58 | + Self::dfs(root, &mut res); |
| 59 | + let (mut x, mut y) = (None, None); |
| 60 | + for i in 0..res.len()-1 { |
| 61 | + if res[i+1] < res[i] { |
| 62 | + y = Some(res[i+1]); |
| 63 | + if x == None { |
| 64 | + x = Some(res[i]); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + Self::recover_tree2(root, x.unwrap(), y.unwrap()); |
| 69 | + } |
| 70 | + |
| 71 | + fn dfs(node: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) { |
| 72 | + if let Some(node) = node { |
| 73 | + Self::dfs(&node.borrow().left, res); |
| 74 | + res.push(node.borrow().val); |
| 75 | + Self::dfs(&node.borrow().right, res); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + fn recover_tree2(root: &mut Option<Rc<RefCell<TreeNode>>>, x: i32, y: i32) { |
| 80 | + if let Some(node) = root { |
| 81 | + Self::recover_tree2(&mut node.borrow_mut().left, x, y); |
| 82 | + if node.borrow().val == x || node.borrow().val == y { |
| 83 | + node.borrow_mut().val = if node.borrow().val == x { y } else { x }; |
| 84 | + } |
| 85 | + Self::recover_tree2(&mut node.borrow_mut().right, x, y); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// submission codes end |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use super::*; |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_99() { |
| 98 | + } |
| 99 | +} |
0 commit comments