use std::collections::BTreeMap; #[derive(Debug)] struct Node { val: i32, left: Option<Box<Node>>, right: Option<Box<Node>>, } impl Node { fn new(val: i32) -> Self { Node { val, left: None, right: None } } } fn bottom_view(root: &Option<Box<Node>>) -> Vec<i32> { use std::collections::VecDeque; let mut map = BTreeMap::new(); let mut queue = VecDeque::new(); if let Some(node) = root { queue.push_back((node, 0)); } while let Some((node, hd)) = queue.pop_front() { map.insert(hd, node.val); if let Some(ref left) = node.left { queue.push_back((left, hd - 1)); } if let Some(ref right) = node.right { queue.push_back((right, hd + 1)); } } map.values().cloned().collect() } fn main() { let mut root = Box::new(Node::new(20)); root.left = Some(Box::new(Node::new(8))); root.right = Some(Box::new(Node::new(22))); root.left.as_mut().unwrap().left = Some(Box::new(Node::new(5))); root.left.as_mut().unwrap().right = Some(Box::new(Node::new(3))); root.right.as_mut().unwrap().right = Some(Box::new(Node::new(25))); root.left.as_mut().unwrap().right.as_mut().unwrap().left = Some(Box::new(Node::new(10))); root.left.as_mut().unwrap().right.as_mut().unwrap().right = Some(Box::new(Node::new(14))); let result = bottom_view(&Some(root)); println!("{:?}", result); }
The bottom view is calculated by tracking the horizontal distance (hd) of each node from the root. The last node encountered at each hd during level order traversal is stored. For this tree, the bottom nodes at hd -2 to 2 are 5, 10, 3, 14, and 25 respectively.
Horizontal distance is a measure of how far left or right a node is from the root. The root has hd 0, left child decreases hd by 1, right child increases hd by 1. This helps identify nodes vertically aligned.
fn bottom_view(root: &Option<Box<Node>>) -> Vec<i32> {
use std::collections::HashMap;
use std::collections::VecDeque;
let mut map = HashMap::new();
let mut queue = VecDeque::new();
if let Some(node) = root {
queue.push_back((node, 0));
}
while let Some((node, hd)) = queue.pop_front() {
map.insert(hd, node.val);
if let Some(ref left) = node.left {
queue.push_back((left, hd - 1));
}
if let Some(ref right) = node.right {
queue.push_back((right, hd + 1));
}
}
let mut result = Vec::new();
for key in map.keys() {
result.push(map[key]);
}
result
}The code uses a HashMap which does not maintain order of keys. Iterating over keys and pushing values results in an unordered bottom view. The bottom view requires nodes sorted by horizontal distance, so a BTreeMap should be used instead.
The bottom view shows the node visible when looking from the bottom. If multiple nodes share the same horizontal distance, the one with the greatest depth (lowest level) is visible.
use std::collections::BTreeMap; #[derive(Debug)] struct Node { val: i32, left: Option<Box<Node>>, right: Option<Box<Node>>, } impl Node { fn new(val: i32) -> Self { Node { val, left: None, right: None } } } fn bottom_view(root: &Option<Box<Node>>) -> Vec<i32> { use std::collections::VecDeque; let mut map = BTreeMap::new(); let mut queue = VecDeque::new(); if let Some(node) = root { queue.push_back((node, 0)); } while let Some((node, hd)) = queue.pop_front() { map.insert(hd, node.val); if let Some(ref left) = node.left { queue.push_back((left, hd - 1)); } if let Some(ref right) = node.right { queue.push_back((right, hd + 1)); } } map.values().cloned().collect() } fn main() { let mut root = Box::new(Node::new(1)); root.left = Some(Box::new(Node::new(2))); root.right = Some(Box::new(Node::new(3))); root.left.as_mut().unwrap().left = Some(Box::new(Node::new(4))); root.left.as_mut().unwrap().right = Some(Box::new(Node::new(5))); root.right.as_mut().unwrap().left = Some(Box::new(Node::new(6))); root.right.as_mut().unwrap().right = Some(Box::new(Node::new(7))); root.left.as_mut().unwrap().right.as_mut().unwrap().right = Some(Box::new(Node::new(8))); root.right.as_mut().unwrap().left.as_mut().unwrap().left = Some(Box::new(Node::new(9))); let result = bottom_view(&Some(root)); println!("{:?}", result); }
The bottom view nodes from left to right correspond to horizontal distances -2 to 3. The nodes at these distances are 4, 2, 9, 6, 8, and 7 respectively after level order traversal updates.
