|
| 1 | +// 1457. Pseudo-Palindromic Paths in a Binary Tree |
| 2 | +// 🟠 Medium |
| 3 | +// |
| 4 | +// https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/ |
| 5 | +// |
| 6 | +// Tags: Bit Manipulation - Tree - Depth-First Search - Breadth-First Search - Binary Tree |
| 7 | + |
| 8 | +use std::cell::RefCell; |
| 9 | +use std::rc::Rc; |
| 10 | + |
| 11 | +// Definition for a binary tree node. |
| 12 | +#[derive(Debug, PartialEq, Eq)] |
| 13 | +pub struct TreeNode { |
| 14 | + pub val: i32, |
| 15 | + pub left: Option<Rc<RefCell<TreeNode>>>, |
| 16 | + pub right: Option<Rc<RefCell<TreeNode>>>, |
| 17 | +} |
| 18 | + |
| 19 | +impl TreeNode { |
| 20 | + #[inline] |
| 21 | + pub fn new(val: i32) -> Self { |
| 22 | + TreeNode { |
| 23 | + val, |
| 24 | + left: None, |
| 25 | + right: None, |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +struct Solution; |
| 31 | +impl Solution { |
| 32 | + /// Use DFS to explore each path in the tree from root to leaf, for each path, keep track of |
| 33 | + /// the values that we see, depending on the use that we want to give the value count, we might |
| 34 | + /// need to use a hashmap as a counter, in this case, we only need to know if we have seen each |
| 35 | + /// value an even or uneven number of times, for that we can use a set, value is present for |
| 36 | + /// uneven number of times, not present for 0 or even number of times. This solution uses the |
| 37 | + /// same logic as the Python solution but iterative DFS and a bitmask instead of recursive DFS |
| 38 | + /// and a hashset to make it a little different. |
| 39 | + /// |
| 40 | + /// Time complexity: O(n) - We visit each value and do O(1) work for each. |
| 41 | + /// Space complexity: O(h) - The stack will grow to the height of the tree, which could be n. |
| 42 | + /// |
| 43 | + /// Runtime 26 ms Beats 100% |
| 44 | + /// Memory 12.12 MB Beats 87.50% |
| 45 | + pub fn pseudo_palindromic_paths(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { |
| 46 | + // A stack with the node and the values bitmask, we need at least 9 bits. |
| 47 | + let mut stack = vec![(root.unwrap(), 0u16)]; |
| 48 | + let mut res = 0; |
| 49 | + while let Some((rc, mut seen)) = stack.pop() { |
| 50 | + let nr = rc.borrow(); |
| 51 | + // Flip the bit for this value. Is guaranteed that 1 <= val <= 9. |
| 52 | + seen ^= 1 << nr.val; |
| 53 | + // Process the children |
| 54 | + match (nr.left.clone(), nr.right.clone()) { |
| 55 | + // No children, we are at a leaf, add 1 if the path values form a palindrome. |
| 56 | + (None, None) => { |
| 57 | + if seen.count_ones() < 2 { |
| 58 | + res += 1; |
| 59 | + } |
| 60 | + } |
| 61 | + (None, Some(child)) | (Some(child), None) => stack.push((child, seen)), |
| 62 | + (Some(left), Some(right)) => { |
| 63 | + stack.push((left, seen)); |
| 64 | + stack.push((right, seen)); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + res |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Tests. |
| 73 | +fn main() { |
| 74 | + // let tests = [(vec![0], 0)]; |
| 75 | + // println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len()); |
| 76 | + // let mut success = 0; |
| 77 | + // for (i, t) in tests.iter().enumerate() { |
| 78 | + // let res = Solution::pseudo_palindromic_paths(t.0.clone()); |
| 79 | + // if res == t.1 { |
| 80 | + // success += 1; |
| 81 | + // println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i); |
| 82 | + // } else { |
| 83 | + // println!( |
| 84 | + // "\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m", |
| 85 | + // i, t.1, res |
| 86 | + // ); |
| 87 | + // } |
| 88 | + // } |
| 89 | + // println!(); |
| 90 | + // if success == tests.len() { |
| 91 | + // println!("\x1b[30;42m✔ All tests passed!\x1b[0m") |
| 92 | + // } else if success == 0 { |
| 93 | + // println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m") |
| 94 | + // } else { |
| 95 | + // println!("\x1b[31mx\x1b[95m {} tests failed!\x1b[0m", tests.len() - success) |
| 96 | + // } |
| 97 | +} |
0 commit comments