use std::collections::{HashMap, VecDeque};
#[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 top_view(root: &Option<Box<Node>>) -> Vec<i32> {
if root.is_none() {
return vec![];
}
let mut queue = VecDeque::new();
let mut hd_map = HashMap::new();
queue.push_back((root.as_ref(), 0));
while let Some((node_opt, hd)) = queue.pop_front() {
if let Some(node) = node_opt {
hd_map.entry(hd).or_insert(node.val);
if node.left.is_some() {
queue.push_back((node.left.as_ref(), hd - 1));
}
if node.right.is_some() {
queue.push_back((node.right.as_ref(), hd + 1));
}
}
}
let mut keys: Vec<_> = hd_map.keys().cloned().collect();
keys.sort();
keys.iter().map(|k| *hd_map[k]).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().right = Some(Box::new(Node::new(4)));
root.left.as_mut().unwrap().right.as_mut().unwrap().right = Some(Box::new(Node::new(5)));
root.left.as_mut().unwrap().right.as_mut().unwrap().right.as_mut().unwrap().right = Some(Box::new(Node::new(6)));
let result = top_view(&Some(root));
for val in result {
print!("{} ", val);
}
}The top view prints the first node encountered at each horizontal distance from left to right.
Nodes 2, 1, 3, and 6 are the first nodes at horizontal distances -1, 0, 1, and 2 respectively.
use std::collections::{HashMap, VecDeque};
#[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 top_view(root: &Option<Box<Node>>) -> Vec<i32> {
if root.is_none() {
return vec![];
}
let mut queue = VecDeque::new();
let mut hd_map = HashMap::new();
queue.push_back((root.as_ref(), 0));
while let Some((node_opt, hd)) = queue.pop_front() {
if let Some(node) = node_opt {
hd_map.entry(hd).or_insert(node.val);
if node.left.is_some() {
queue.push_back((node.left.as_ref(), hd - 1));
}
if node.right.is_some() {
queue.push_back((node.right.as_ref(), hd + 1));
}
}
}
let mut keys: Vec<_> = hd_map.keys().cloned().collect();
keys.sort();
keys.iter().map(|k| *hd_map[k]).collect()
}
fn main() {
let mut root = Box::new(Node::new(10));
root.left = Some(Box::new(Node::new(20)));
root.right = Some(Box::new(Node::new(30)));
root.left.as_mut().unwrap().left = Some(Box::new(Node::new(40)));
root.left.as_mut().unwrap().right = Some(Box::new(Node::new(50)));
root.right.as_mut().unwrap().left = Some(Box::new(Node::new(60)));
root.right.as_mut().unwrap().right = Some(Box::new(Node::new(70)));
let result = top_view(&Some(root));
for val in result {
print!("{} ", val);
}
}The top view includes nodes at horizontal distances -2, -1, 0, 1, and 2.
These correspond to nodes 40, 20, 10, 30, and 70 respectively.
use std::collections::{HashMap, VecDeque};
#[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 top_view(root: &Option<Box<Node>>) -> Vec<i32> {
if root.is_none() {
return vec![];
}
let mut queue = VecDeque::new();
let mut hd_map = HashMap::new();
queue.push_back((root.as_ref(), 0));
while let Some((node_opt, hd)) = queue.pop_front() {
if let Some(node) = node_opt {
hd_map.insert(hd, node.val); // <-- Problem here
if node.left.is_some() {
queue.push_back((node.left.as_ref(), hd - 1));
}
if node.right.is_some() {
queue.push_back((node.right.as_ref(), hd + 1));
}
}
}
let mut keys: Vec<_> = hd_map.keys().cloned().collect();
keys.sort();
keys.iter().map(|k| *hd_map[k]).collect()
}The code uses hd_map.insert which replaces the value for a horizontal distance every time a node is visited.
For top view, only the first node at each horizontal distance should be recorded.
Horizontal distance is a measure of how far left or right a node is from the root.
It starts at 0 for the root, decreases by 1 for left child, and increases by 1 for right child.
use std::collections::{HashMap, VecDeque};
#[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 top_view(root: &Option<Box<Node>>) -> Vec<i32> {
if root.is_none() {
return vec![];
}
let mut queue = VecDeque::new();
let mut hd_map = HashMap::new();
queue.push_back((root.as_ref(), 0));
while let Some((node_opt, hd)) = queue.pop_front() {
if let Some(node) = node_opt {
hd_map.entry(hd).or_insert(node.val);
if node.left.is_some() {
queue.push_back((node.left.as_ref(), hd - 1));
}
if node.right.is_some() {
queue.push_back((node.right.as_ref(), hd + 1));
}
}
}
let mut keys: Vec<_> = hd_map.keys().cloned().collect();
keys.sort();
keys.iter().map(|k| *hd_map[k]).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().left = Some(Box::new(Node::new(8)));
root.left.as_mut().unwrap().right.as_mut().unwrap().right = Some(Box::new(Node::new(9)));
let result = top_view(&Some(root));
for val in result {
print!("{} ", val);
}
}The horizontal distances and first nodes encountered are:
- -2: 4
- -1: 2
- 0: 1
- 1: 3
- 2: 7
Nodes 5, 6, 8, 9 are hidden behind these in top view.
