forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a098eb1
commit 7feca7b
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
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,6 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
project(C) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
add_executable(C main.cpp) |
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,70 @@ | ||
/// Source : https://leetcode.com/problems/maximum-average-subtree/ | ||
/// Author : liuyubobobo | ||
/// Time : 2019-07-13 | ||
|
||
#include <iostream> | ||
#include <unordered_map> | ||
|
||
using namespace std; | ||
|
||
|
||
/// DFS | ||
/// Time Complexity: O(n) | ||
/// Space Complexity: O(n) | ||
|
||
/// Definition for a binary tree node. | ||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
}; | ||
|
||
|
||
class Solution { | ||
|
||
private: | ||
unordered_map<TreeNode*, int> sum; | ||
unordered_map<TreeNode*, int> sz; | ||
|
||
public: | ||
double maximumAverageSubtree(TreeNode* root) { | ||
|
||
dfsSum(root); | ||
dfsSz(root); | ||
return dfsRes(root); | ||
} | ||
|
||
private: | ||
int dfsSum(TreeNode* node){ | ||
|
||
if(!node) return 0; | ||
int left = dfsSum(node->left); | ||
int right = dfsSum(node->right); | ||
sum[node] = left + right + node->val; | ||
return sum[node]; | ||
} | ||
|
||
int dfsSz(TreeNode* node){ | ||
|
||
if(!node) return 0; | ||
int left = dfsSz(node->left); | ||
int right = dfsSz(node->right); | ||
sz[node] = left + right + 1; | ||
return sz[node]; | ||
} | ||
|
||
double dfsRes(TreeNode* node){ | ||
|
||
if(!node) return 0; | ||
double left = dfsRes(node->left); | ||
double right = dfsRes(node->right); | ||
return max(max(left, right), (double)sum[node] / sz[node]); | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |
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 |
---|---|---|
|
@@ -764,4 +764,5 @@ email: [[email protected]](mailto:[email protected]) | |
| | | | | | | | ||
| 1118 | [Number of Days in a Month](https://leetcode.com/problems/number-of-days-in-a-month/) | [无] | [C++](1118-Number-of-Days-in-a-Month/cpp-1118/) | | | | ||
| 1119 | [Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/) | [无] | [C++](1119-Remove-Vowels-from-a-String/cpp-1119/) | | | | ||
| 1120 | [Maximum Average Subtree](https://leetcode.com/problems/maximum-average-subtree/) | [无] | [C++](1120-Maximum-Average-Subtree/cpp-1120/) | | | | ||
| | | | | | | |