Skip to content

Commit 068b9fb

Browse files
committed
Day - 74 work
1 parent 5fe74a2 commit 068b9fb

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-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 | 107 |
10-
| Current Streak | 73 days |
11-
| Longest Streak | 73 ( August 17, 2015 - October 28, 2015 ) |
9+
| Total Problems | 109 |
10+
| Current Streak | 74 days |
11+
| Longest Streak | 74 ( August 17, 2015 - October 29, 2015 ) |
1212

1313
</center>
1414

@@ -114,6 +114,8 @@ Include contains single header implementation of data structures and some algori
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)|
115115
| Convert a sorted array to balanced binary search tree.| [sortedArrayToBST.cpp](tree_problems/sortedArrayToBST.cpp)|
116116
| Given a binary tree, generate sum of each vertical column.|[verticalSum.cpp](tree_problems/verticalSum.cpp)|
117+
| Given a binary tree and key, node with key exists in tree. Find all the ancestors of the node with key, ancestor here are the nodes which are in straight path from node to root.| [node_ancestors_in_root_path.cpp](tree_problems/node_ancestors_in_root_path.cpp)|
118+
| Given a binary tree and key, return the level of the node with key. Root is at level 1, and if node with key does not exists in tree, return 0| [level_of_node.cpp](tree_problems/level_of_node.cpp)|
117119

118120

119121
### String Problems

tree_problems/level_of_node.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Given a Binary Tree and a key, write a function that returns level of the key.
3+
* 3
4+
* / \
5+
* 2 5
6+
* / \
7+
* 1 4
8+
*
9+
* Here 3 is on level 1
10+
* 2 and 5 have level 2
11+
* 1 and 4 have level 3
12+
* If key not in tree, return 0
13+
*
14+
*/
15+
#include <iostream>
16+
struct Node {
17+
int data;
18+
Node * left;
19+
Node * right;
20+
Node( int d ) : data{ d }, left{ nullptr }, right{ nullptr } { }
21+
};
22+
23+
int get_node_level_util( Node * root, int key, int level ) {
24+
if ( root == nullptr ) {
25+
return 0;
26+
}
27+
if ( root->data == key ) {
28+
return level;
29+
}
30+
int downlevel = get_node_level_util( root->left, key, level + 1);
31+
if ( downlevel != 0 ) {
32+
return downlevel;
33+
}
34+
downlevel = get_node_level_util( root->right, key, level + 1);
35+
return downlevel;
36+
}
37+
38+
int get_node_level( Node * root, int key ) {
39+
return get_node_level_util( root, key, 1 );
40+
}
41+
42+
void printLevels(Node * root) {
43+
for ( int i = 1; i <= 6; ++i ) {
44+
std::cout << "Level of node with key as " << i << " is " << get_node_level( root, i ) << std::endl;
45+
}
46+
}
47+
48+
int main() {
49+
Node * root = new Node(3);
50+
root->left = new Node(2);
51+
root->right = new Node(5);
52+
root->left->left = new Node(1);
53+
root->left->right = new Node(4);
54+
printLevels(root);
55+
return 0;
56+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Given a binary Tree
3+
* 1
4+
* / \
5+
* 2 3
6+
* / \
7+
* 4 5
8+
* \
9+
* 6
10+
*
11+
* Ancestor of node 6 in root path would be 4, 2, and 1.
12+
*
13+
*/
14+
15+
#include <iostream>
16+
#include <vector>
17+
18+
struct Node {
19+
int data;
20+
Node * left;
21+
Node * right;
22+
Node( int d ) : data{ d }, left{ nullptr }, right{ nullptr } { }
23+
};
24+
25+
26+
bool get_ancestors_util( Node * root, int key, std::vector<int> & ancestors ) {
27+
if ( root == nullptr ) {
28+
return false;
29+
}
30+
31+
if ( root->data == key ) {
32+
return true;
33+
}
34+
35+
if ( get_ancestors_util(root->left, key, ancestors) ||
36+
get_ancestors_util(root->right, key, ancestors) ) {
37+
ancestors.push_back(root->data);
38+
return true;
39+
}
40+
return false;
41+
}
42+
43+
std::vector<int> get_ancestors( Node * root, int key ) {
44+
std::vector<int> ancestors;
45+
get_ancestors_util(root, key, ancestors);
46+
return ancestors;
47+
}
48+
49+
void printAncestors( Node * root ) {
50+
std::vector<int> vec;
51+
for ( int i = 1; i <= 6; ++i ) {
52+
std::cout << "Ancestor of node with key " << i << " are:";
53+
vec = get_ancestors( root, i );
54+
for ( auto v : vec ) {
55+
std::cout << v << " ";
56+
}
57+
std::cout << std::endl;
58+
vec.clear();
59+
}
60+
}
61+
62+
int main() {
63+
Node * root = new Node(1);
64+
root->left = new Node(2);
65+
root->right = new Node(3);
66+
root->left->left = new Node(4);
67+
root->left->right = new Node(5);
68+
root->left->left->right = new Node(6);
69+
printAncestors(root);
70+
return 0;
71+
}

0 commit comments

Comments
 (0)