-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solution(cpp): 515. Find Largest Value in Each Tree Row
515. Find Largest Value in Each Tree Row - C++
- Loading branch information
Showing
3 changed files
with
63 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Solution | ||
{ | ||
public: | ||
vector<int> largestValues(TreeNode *root) | ||
{ | ||
if (!root) | ||
return {}; | ||
|
||
std::vector<int> result; | ||
std::queue<TreeNode *> queue; | ||
queue.push(root); | ||
|
||
while (!queue.empty()) | ||
{ | ||
int curr_level_size = queue.size(); | ||
int max_val = INT_MIN; | ||
|
||
for (int i = 0; i < curr_level_size; ++i) | ||
{ | ||
TreeNode *node = queue.front(); | ||
queue.pop(); | ||
max_val = std::max(max_val, node->val); | ||
|
||
if (node->left) | ||
queue.push(node->left); | ||
if (node->right) | ||
queue.push(node->right); | ||
} | ||
|
||
result.push_back(max_val); | ||
} | ||
|
||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
## Approach | ||
|
||
The approach used here is to perform a level-wise traversal of the binary tree while keeping track of the largest value in each level. This is achieved using a queue for level order traversal. | ||
|
||
### Initialization | ||
- Begin with initializing an empty queue. | ||
- Add the root node of the binary tree to the queue. This will serve as the starting point for the traversal. | ||
|
||
### Level-wise Traversal | ||
- Continue processing nodes while the queue is not empty. | ||
- For each level, calculate the size of the current level (i.e., the number of nodes in that level). This helps in distinguishing between nodes of different levels. | ||
|
||
### Capture Maximum | ||
- For each level, initialize a variable `max_val` with the smallest possible integer (INT_MIN). This variable is used to keep track of the maximum value in the current level. | ||
- As we process each node in the current level, we update `max_val` to be the maximum between the node's value and the current `max_val`. | ||
|
||
### Child Processing | ||
- After processing a node, add its left and right children (if they exist) to the queue. This ensures that the next level of nodes will be processed in the subsequent iterations. | ||
|
||
### Result Update | ||
- Once all nodes of a level are processed, the maximum value for that level (`max_val`) is added to the result list. | ||
|
||
### Complexity | ||
|
||
- Time complexity: O(n) where n is the number of nodes in the binary tree. Each node is processed once, and the maximum value at each level is determined in constant time. So, the overall time taken is linear with respect to the number of nodes. | ||
- Space complexity: O(n). In the worst case, the queue can hold all leaf nodes. In a balanced binary tree, the last level can have n/2 nodes. Thus, the space complexity is linear. | ||
|
||
|