-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay21.cpp
More file actions
87 lines (70 loc) · 2.09 KB
/
Day21.cpp
File metadata and controls
87 lines (70 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
This problem was asked by Yelp.
The horizontal distance of a binary tree node describes how far left or right the node will be when the tree is printed out.
More rigorously, we can define it as follows:
The horizontal distance of the root is 0.
The horizontal distance of a left child is hd(parent) - 1.
The horizontal distance of a right child is hd(parent) + 1.
For example, for the following tree, hd(1) = -2, and hd(6) = 0.
5
/ \
3 7
/ \ / \
1 4 6 9
/ /
0 8
The bottom view of a tree, then, consists of the lowest node at each horizontal distance. If there are two nodes at the same depth and horizontal distance, either is acceptable.
For this tree, for example, the bottom view could be [0, 1, 3, 6, 8, 9].
Given the root to a binary tree, return its bottom view.
*/
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
void dfs(map<int, pair<int, int>> &umap, TreeNode *node, int HD, int depth)
{
if (node == nullptr)
{
return;
}
if (umap.find(HD) == umap.end() || depth >= umap[HD].second)
{
umap[HD] = {node->val, depth};
}
dfs(umap, node->left, HD - 1, depth - 1);
dfs(umap, node->right, HD + 1, depth + 1);
}
vector<int> bottomView(TreeNode *root)
{
vector<int> res;
map<int, pair<int, int>> umap;
dfs(umap, root, 0, 0);
for (auto it : umap)
{
res.push_back(it.second.first);
}
return res;
}
int main()
{
TreeNode *root = new TreeNode(5);
root->left = new TreeNode(3);
root->right = new TreeNode(7);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(4);
root->right->left = new TreeNode(6);
root->right->right = new TreeNode(9);
root->left->left->left = new TreeNode(0);
root->right->right->left = new TreeNode(8);
vector<int> result = bottomView(root);
for (int i : result)
{
cout << i << " ";
}
return 0;
}