-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLongestPathInBinaryTree.cpp
141 lines (116 loc) · 3.44 KB
/
LongestPathInBinaryTree.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// https://www.careercup.com/question?id=5140810577215488
// https://massivealgorithms.blogspot.com/2015/11/longest-increasing-path-in-n-ary-tree.html
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
class Tree
{
private:
class Node
{
public:
int key;
Node* left;
Node* right;
Node(int k) : key {k}, left{nullptr}, right{nullptr} { }
};
Node* root;
using Path = std::vector<Node *>;
void constructTree(std::vector<int>& keys)
{
std::queue<Node*> nodeQueue;
if (root == nullptr) {
root = new Node(keys[0]);
}
nodeQueue.push(root);
int keyIndex = 1;
while (nodeQueue.size() != 0) {
Node* current = nodeQueue.front();
nodeQueue.pop();
if (keyIndex < keys.size() && keys[keyIndex] != INT_MIN) {
current->left = new Node(keys[keyIndex]);
nodeQueue.push(current->left);
}
keyIndex++;
if (keyIndex < keys.size() && keys[keyIndex] != INT_MIN) {
current->right = new Node(keys[keyIndex]);
nodeQueue.push(current->right);
}
keyIndex++;
}
}
void preorder(Node*& node, std::vector<Node*>& path) {
if (node == nullptr) {
return;
}
path.push_back(node);
preorder(node->left, path);
preorder(node->right, path);
}
void print(std::vector<Node*>& path)
{
for (auto node : path) {
std::cout << node->key << " ";
}
std::cout << "\n";
}
void longestIncreasingPath(Node* node, int currentLength, int expected, int& maxLength,
Path& longestPath, Path currentPath)
{
if (node == nullptr) {
return;
}
if (node->key >= expected) {
currentLength++;
} else {
currentLength = 1;
currentPath.clear();
}
if (currentLength > maxLength) {
longestPath = currentPath;
}
currentPath.push_back(node);
maxLength = std::max(currentLength, maxLength);
longestIncreasingPath(node->left, currentLength, node->key + 1, maxLength, longestPath, currentPath);
longestIncreasingPath(node->right, currentLength, node->key + 1, maxLength, longestPath, currentPath);
}
public:
Tree() : root{nullptr} { }
Tree(std::vector<int>& keys) : root{nullptr}
{
if (keys.size() == 0) {
return;
}
constructTree(keys);
}
void preorder()
{
std::vector<Node*> path;
preorder(root, path);
print(path);
}
int longestConsecutivePath()
{
if (root == nullptr) {
return 0;
}
int length = 0;
Path longestPath {};
Path currentPath {};
longestIncreasingPath(root, 0, root->key, length, longestPath, currentPath);
print(longestPath);
return length;
}
};
int main()
{
std::vector<int> keys {1, 2, 3, 4, 5, 6, INT_MIN, 7, INT_MIN, 8, 9, INT_MIN, 10, 11};
Tree tree(keys);
std::cout << "Inorder traversal: ";
tree.preorder();
std::cout << "Longest path length: ";
tree.longestConsecutivePath();
// std::cout << "\n";
return 0;
}