Skip to content

Commit 25aa888

Browse files
committed
Day - 73 work
1 parent ea06bc1 commit 25aa888

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 105 |
10-
| Current Streak | 72 days |
11-
| Longest Streak | 72 ( August 17, 2015 - October 27, 2015 ) |
9+
| Total Problems | 107 |
10+
| Current Streak | 73 days |
11+
| Longest Streak | 73 ( August 17, 2015 - October 28, 2015 ) |
1212

1313
</center>
1414

@@ -112,6 +112,8 @@ Include contains single header implementation of data structures and some algori
112112
|Given a binary tree, print out all of its root-to-leaf paths one per line.| [printAllRootToLeafPath.cpp](tree_problems/printAllRootToLeafPath.cpp)
113113
|Determine if a tree is sum tree. A SumTree is a Binary Tree where the value of a node is equal to sum of the nodes present in its left subtree and right subtree. An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree.| [sumTree.cpp](tree_problems/sumTree.cpp)|
114114
|Convert a tree to sumTree, such that each node is sum of left and right subtree of the original tree.| [convert_to_sum_tree.cpp](tree_problems/convert_to_sum_tree.cpp)|
115+
| Convert a sorted array to balanced binary search tree.| [sortedArrayToBST.cpp](tree_problems/sortedArrayToBST.cpp)|
116+
| Given a binary tree, generate sum of each vertical column.|[verticalSum.cpp](tree_problems/verticalSum.cpp)|
115117

116118

117119
### String Problems

tree_problems/sortedArrayToBST.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Given a sorted array, convert it balaned binary search tree.
3+
*/
4+
5+
#include<iostream>
6+
#include<vector>
7+
8+
struct Node {
9+
int data;
10+
Node * left;
11+
Node * right;
12+
Node( int d ) : data{ d }, left{ nullptr }, right{ nullptr } { }
13+
};
14+
15+
Node * sortedArrayToBST( std::vector<int> & arr, int start, int end ) {
16+
if ( start > end ) {
17+
return nullptr;
18+
}
19+
int mid = ( start + end )/2;
20+
Node * node = new Node(arr[mid]);
21+
node->left = sortedArrayToBST( arr, start, mid - 1 );
22+
node->right = sortedArrayToBST( arr, mid + 1, end );
23+
return node;
24+
}
25+
26+
void printArray( std::vector<int> & arr ) {
27+
for ( auto i : arr ) {
28+
std::cout << i << " ";
29+
}
30+
std::cout << std::endl;
31+
}
32+
33+
void printTreeInOrder( Node * root ) {
34+
if( root ) {
35+
printTreeInOrder( root->left );
36+
std::cout << root->data << " ";
37+
printTreeInOrder( root->right );
38+
}
39+
}
40+
41+
int main() {
42+
std::vector<int> arr{ 1, 2, 3, 4, 5, 6, 7, 8 };
43+
std::cout << "Arr :";
44+
printArray(arr);
45+
Node * root = sortedArrayToBST(arr, 0, arr.size() - 1);
46+
std::cout << "Tree :";
47+
printTreeInOrder(root);
48+
std::cout << std::endl;
49+
return 0;
50+
}

tree_problems/verticalSum.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Given a binary Tree
3+
* 1
4+
* / \
5+
* 2 3
6+
* / \ / \
7+
* 4 5 6 7
8+
*
9+
* Find vertical sums of the tree.
10+
* Align the nodes in vertical line.
11+
* Here
12+
* The tree has 5 vertical lines
13+
*
14+
* Vertical-Line-1 has only one node 4 => vertical sum is 4
15+
* Vertical-Line-2: has only one node 2=> vertical sum is 2
16+
* Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12
17+
* Vertical-Line-4: has only one node 3 => vertical sum is 3
18+
* Vertical-Line-5: has only one node 7 => vertical sum is 7
19+
*
20+
* So expected output is 4, 2, 12, 3 and 7
21+
*
22+
*/
23+
24+
#include <iostream>
25+
#include <unordered_map>
26+
#include <vector>
27+
#include <algorithm>
28+
#include <map>
29+
30+
struct Node {
31+
int data;
32+
Node * left;
33+
Node * right;
34+
Node ( int d ) : data{ d }, left{ nullptr }, right{ nullptr } { }
35+
};
36+
37+
void vertical_sum_util( Node * root, int dist, std::unordered_map<int,int> & vmap) {
38+
if ( root == nullptr ) {
39+
return ;
40+
}
41+
if ( vmap.find(dist) != vmap.end() ) {
42+
vmap[dist] += root->data;
43+
} else {
44+
vmap[dist] = root->data;
45+
}
46+
vertical_sum_util( root->left, dist-1, vmap);
47+
vertical_sum_util( root->right, dist+1, vmap);
48+
}
49+
50+
void vertical_sum( Node * root ) {
51+
if ( root == nullptr ) {
52+
return;
53+
}
54+
std::unordered_map<int, int> vmap;
55+
vertical_sum_util( root, 0, vmap );
56+
std::map<int, int> ordered( vmap.begin(), vmap.end());
57+
std::cout << "Printing vertical sum values, first column represent the position of vertical column with respect to root(0)\n"
58+
<< "and second column represent vertical sum for that column\n";
59+
for ( auto it = ordered.begin(); it != ordered.end(); ++it ) {
60+
std::cout << it->first << " " << it->second << std::endl;
61+
}
62+
}
63+
64+
int main() {
65+
Node * root = new Node(1);
66+
root->left = new Node(2);
67+
root->right = new Node(3);
68+
root->left->left = new Node(4);
69+
root->left->right = new Node(5);
70+
root->right->left = new Node(6);
71+
root->right->right = new Node(7);
72+
vertical_sum(root);
73+
return 0;
74+
75+
}

0 commit comments

Comments
 (0)