File tree Expand file tree Collapse file tree 1 file changed +4
-3
lines changed Expand file tree Collapse file tree 1 file changed +4
-3
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ class TreeNode
1919
2020vector<int > getTopView (TreeNode<int > *root)
2121{
22+ // hd means horizontal distance which is nothing but the x-coordinate of the node (or we can say it's a vertical level)
2223 map<int , int > hdToNode;
2324 queue<pair<TreeNode<int > *, int >> q;
2425 q.push ({root, 0 });
@@ -31,17 +32,17 @@ vector<int> getTopView(TreeNode<int> *root)
3132 int hd = temp.second ;
3233
3334 // if no entry for this hd exists, then add it in the map
34- if (hdToNode. find (hd) == hdToNode. end () )
35+ if (! hdToNode[hd] )
3536 {
3637 hdToNode[hd] = frontNode->data ;
3738 }
3839
3940 // children
40- if (frontNode->left != NULL )
41+ if (frontNode->left )
4142 {
4243 q.push ({frontNode->left , hd - 1 });
4344 }
44- if (frontNode->right != NULL )
45+ if (frontNode->right )
4546 {
4647 q.push ({frontNode->right , hd + 1 });
4748 }
You can’t perform that action at this time.
0 commit comments