forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol.cpp
41 lines (35 loc) · 834 Bytes
/
sol.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
struct node{
// b tree node data structure
int data;
node *left, *right;
};
node *createNode(int data){
// fn to create a b tree node
node *temp = new node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int minPathSum(node *root){
// min path root to leaf sum
if (root==NULL) return 0;
return root->data + std::min(minPathSum(root->left), minPathSum(root->right));
}
void test(){
// builds and runs test cases
node *root = createNode(10);
root->left = createNode(5);
root->left->right = createNode(2);
root->right = createNode(5);
root->right->right = createNode(1);
root->right->right->left = createNode(-1);
std::cout << minPathSum(root) << std::endl;
}
int main(){
// run the test
test();
return 0;
}