diff --git a/src/problem/mod.rs b/src/problem/mod.rs index ca81fb6..639d456 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -45,4 +45,5 @@ mod p993_cousins_in_binary_tree; mod p236_lowest_common_ancestor_of_a_binary_tree; mod p94_binary_tree_inorder_traversal; mod p144_binary_tree_preorder_traversal; -mod p145_binary_tree_postorder_traversal; \ No newline at end of file +mod p145_binary_tree_postorder_traversal; +mod p987_vertical_order_traversal_of_a_binary_tree; \ No newline at end of file diff --git a/src/problem/p987_vertical_order_traversal_of_a_binary_tree.rs b/src/problem/p987_vertical_order_traversal_of_a_binary_tree.rs new file mode 100644 index 0000000..4204aeb --- /dev/null +++ b/src/problem/p987_vertical_order_traversal_of_a_binary_tree.rs @@ -0,0 +1,121 @@ +/** + * [987] Vertical Order Traversal of a Binary Tree + */ +pub struct Solution {} + +use crate::util::tree::{TreeNode, to_tree}; + +// submission codes start here + +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; + +#[derive(Debug)] +struct Node { + x: i32, + y: i32, + val: i32 +} + +struct DFS { + nodes: Vec +} + +impl DFS { + fn new() -> DFS { + DFS { + nodes: Vec::new() + } + } + + fn search(&mut self, node: &Rc>, x: i32, y: i32) { + self.nodes.push(Node { + val: node.borrow().val, + x, + y + }); + + if let Some(left) = &node.borrow().left { + self.search(left, x - 1, y + 1); + } + + if let Some(right) = &node.borrow().right { + self.search(right, x + 1, y + 1); + } + } +} + +impl Solution { + pub fn vertical_traversal(root: Option>>) -> Vec> { + let root = if let Some(r) = root { + r + } else { + return vec![]; + }; + + let mut result = Vec::new(); + + let mut dfs = DFS::new(); + dfs.search(&root, 0, 0); + + dfs.nodes.sort_unstable_by(|a, b| { + if a.x != b.x { + a.x.cmp(&b.x) + } else if a.y != b.y { + a.y.cmp(&b.y) + } else { + a.val.cmp(&b.val) + } + }); + + let mut last = None; + for node in dfs.nodes { + if let Some((num, pos)) = last { + if num == node.x { + let mut array: &mut Vec = &mut result[pos]; + array.push(node.val); + } else { + last = Some((node.x, pos + 1)); + result.push(vec![node.val]); + } + } else { + last = Some((node.x, 0)); + result.push(vec![node.val]); + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_987() { + assert_eq!(Solution::vertical_traversal( + to_tree(vec![Some(3), Some(9), Some(20), None, None, Some(15), Some(7)]) + ), vec![vec![9], vec![3, 15], vec![20], vec![7]]); + } +}