-
Notifications
You must be signed in to change notification settings - Fork 0
/
0515-Find_Largest_Value_in_Each_Tree_Row.cpp
147 lines (135 loc) · 3.24 KB
/
0515-Find_Largest_Value_in_Each_Tree_Row.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
142
143
144
145
146
147
/*******************************************************************************
* 0515-Find_Largest_Value_in_Each_Tree_Row.cpp
* Billy.Ljm
* 24 October 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/find-largest-value-in-each-tree-row/
*
* Given the root of a binary tree, return an array of the largest value in each
* row of the tree (0-indexed).
*
* ===========
* My Approach
* ===========
* We just have to iterate through every node and remember their depths to
* recognise the largest value in each row. I'll use depth-first search.
*
* This has a time complexity of O(n), and space complexity of O(n), where n is
* the number of nodes in the tree.
******************************************************************************/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Definition for a binary tree node
*/
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
/**
* Create binary tree from vector of row-by-row node values
*
* @param vec vector to create binary tree from
*
* @return root of created binary tree
*/
TreeNode* createTree(std::vector<int> vec) {
if (vec.empty()) {
return nullptr;
}
TreeNode* root = new TreeNode(vec[0]);
std::queue<TreeNode*> q;
q.push(root);
int i = 1;
while (!q.empty() && i < vec.size()) {
TreeNode* node = q.front();
q.pop();
if (vec[i] != -1) {
node->left = new TreeNode(vec[i]);
q.push(node->left);
}
i++;
if (i < vec.size() && vec[i] != -1) {
node->right = new TreeNode(vec[i]);
q.push(node->right);
}
i++;
}
return root;
}
/**
* Deletes a binary tree
*
* @param root root of the binary tree
*/
void deleteTree(TreeNode* root) {
if (root == nullptr) {
return;
}
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
/**
* Solution
*/
class Solution {
private:
void dfs(TreeNode* root, int depth, vector<int>& maxvals) {
// remember max value
if (depth + 1 > maxvals.size()) maxvals.push_back(root->val);
else maxvals[depth] = max(maxvals[depth], root->val);
// recurse dfs
if (root->right != nullptr) dfs(root->right, depth + 1, maxvals);
if (root->left != nullptr) dfs(root->left, depth + 1, maxvals);
}
public:
vector<int> largestValues(TreeNode* root) {
vector<int> maxvals(0);
if(root != nullptr) dfs(root, 0, maxvals);
return maxvals;
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
vector<int> vals;
TreeNode* root;
// test case 1
vals = { 1,3,2,5,3,-1,9 };
root = createTree(vals);
std::cout << "largestValues(" << vals << ") = ";
std::cout << sol.largestValues(root) << std::endl;
deleteTree(root);
// test case 2
vals = { 1,2,3 };
root = createTree(vals);
std::cout << "largestValues(" << vals << ") = ";
std::cout << sol.largestValues(root) << std::endl;
deleteTree(root);
return 0;
}