Skip to content

Commit 7ed5547

Browse files
committed
Day-34: Tree problems
1 parent 8dd42ea commit 7ed5547

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

README.md

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

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 50 |
8-
| Current Streak | 33 |
9-
| Longest Streak | 33 ( August 17, 2015 - September 18, 2015 ) |
7+
| Total Problems | 52 |
8+
| Current Streak | 34 |
9+
| Longest Streak | 34 ( August 17, 2015 - September 19, 2015 ) |
1010

1111
</center>
1212

@@ -78,6 +78,8 @@ Include contains single header implementation of data structures and some algori
7878
|Recursive Level order traveral of Tree | [levelOrderTraversalRecursive.cpp](tree_problems/levelOrderTraversalRecursive.cpp)|
7979
|ZigZag Traversal of Tree | [zigZagTraversal.cpp](tree_problems/zigZagTraversal.cpp)|
8080
|Predecessor and Successor of a given node in Binary Search Tree | [predecessorSuccessor.cpp](tree_problems/predecessorSuccessor.cpp)|
81+
|Given values of two nodes in a Binary Search Tree, find the Lowest Common Ancestor (LCA). Assume that both the values exist in the tree.| [lowest-common-ancestor.cpp](tree_problems/lowest-common-ancestor.cpp)|
82+
|Given a binary tree, print out all of its root-to-leaf paths one per line.| [printAllRootToLeafPath.cpp](tree_problems/printAllRootToLeafPath.cpp)
8183

8284

8385
### String Problems
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Given a binary search tree, determine the lowest common ancestor of two given nodes with data
3+
* key1 and key2. Assumption is key1 and key2 exists in tree
4+
* Example
5+
* 20
6+
* / \
7+
* 8 22
8+
* / \
9+
* 4 12
10+
* / \
11+
* 10 14
12+
*
13+
* In the above tree, LCA of 10 and 14 is 12
14+
* similarly 4 and 22 will have LCA 20
15+
*/
16+
17+
#include <iostream>
18+
19+
struct Node {
20+
int data;
21+
Node * left = nullptr;
22+
Node * right = nullptr;
23+
Node (int d) :
24+
data{ d }, left{ nullptr }, right{ nullptr } { }
25+
};
26+
27+
Node * lca ( Node * root, int key1, int key2 ) {
28+
if ( root == nullptr ) {
29+
return nullptr;
30+
}
31+
if (root->data > key1 && root->data > key2) {
32+
return lca(root->left, key1, key2);
33+
} else if ( root->data < key1 && root->data < key2 ) {
34+
return lca(root->right, key1, key2);
35+
}
36+
return root;
37+
}
38+
39+
40+
void inorder(Node * root)
41+
{
42+
if (root) {
43+
inorder(root->left);
44+
std::cout << root->data << " ";
45+
inorder(root->right);
46+
}
47+
}
48+
49+
int main()
50+
{
51+
Node * root = new Node(20);
52+
root->right = new Node(22);
53+
root->left = new Node(8);
54+
root->left->left = new Node(4);
55+
root->left->right= new Node(12);
56+
root->left->right->right = new Node(14);
57+
root->left->right->left = new Node(10);
58+
std::cout << "Inorder traversal of the tree:";
59+
inorder(root);
60+
std::cout << std::endl;
61+
62+
std::cout << std::endl;
63+
std::cout << "LCA of nodes with data 10 and 14 is :"
64+
<< (lca(root, 14, 10))->data << std::endl;
65+
std::cout << "LCA of nodes with data 14 and 8 is :"
66+
<< (lca(root, 14, 8))->data << std::endl;
67+
std::cout << "LCA of root with data 4 and 22 is :"
68+
<< (lca(root, 4, 22))->data << std::endl;
69+
std::cout << "LCA of root with data 14 and 10 is :"
70+
<< (lca(root, 10, 14))->data << std::endl;
71+
return 0;
72+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Given a binary tree, print out all root to leaf path, one per line.
3+
* Example :
4+
* 1
5+
* / \
6+
* 2 3
7+
* / \
8+
* 4 5
9+
* Possible paths in above tree:
10+
* 1 2 4
11+
* 1 2 5
12+
* 1 3
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+
void printPath( const std::vector<int> & vec )
27+
{
28+
for (const int i : vec) {
29+
std::cout << i << " ";
30+
}
31+
std::cout << std::endl;
32+
}
33+
34+
void printPaths( Node * node, std::vector<int> paths )
35+
{
36+
if ( node == nullptr ) {
37+
return;
38+
}
39+
//push data to path
40+
paths.push_back(node->data);
41+
//leaf node
42+
if ( node->left == nullptr && node->right == nullptr ) {
43+
printPath(paths);
44+
} else {
45+
printPaths(node->left, paths);
46+
printPaths(node->right, paths);
47+
}
48+
}
49+
50+
int main()
51+
{
52+
Node * root = new Node(1);
53+
root->left = new Node(2);
54+
root->right = new Node(3);
55+
root->left->left = new Node(4);
56+
root->left->right = new Node(5);
57+
std::vector<int> vec;
58+
printPaths(root, vec);
59+
return 0;
60+
}

0 commit comments

Comments
 (0)