binary_tree_max_path.cpp
This commit is contained in:
57
puzzles/interviews/training/binary_tree_max_path.cpp
Normal file
57
puzzles/interviews/training/binary_tree_max_path.cpp
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
|
||||||
|
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
|
||||||
|
VIM-: let b:cppflags=g:Iboost.g:Itbb
|
||||||
|
VIM-: let b:ldflags=g:Lboost.g:Ltbb
|
||||||
|
VIM-: let b:ldlibpath=g:Bboost.g:Btbb
|
||||||
|
VIM-: let b:argv=""
|
||||||
|
*/
|
||||||
|
#include <iostream>
|
||||||
|
#include <exception>
|
||||||
|
#include <chrono>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
struct node
|
||||||
|
{
|
||||||
|
node * left;
|
||||||
|
node * right;
|
||||||
|
int value;
|
||||||
|
};
|
||||||
|
|
||||||
|
long long max_value_of_path_sum( node * root )
|
||||||
|
{
|
||||||
|
if ( !root ) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto lm = max_value_of_path_sum( root->left );
|
||||||
|
auto rm = max_value_of_path_sum( root->right );
|
||||||
|
|
||||||
|
return root->value + std::max(lm, rm);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main ( void )
|
||||||
|
{try{
|
||||||
|
auto begin = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
|
|
||||||
|
//......
|
||||||
|
|
||||||
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> seconds = end - begin;
|
||||||
|
std::cout << "Time: " << seconds.count() << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
catch ( const std::exception& e )
|
||||||
|
{
|
||||||
|
std::cerr << std::endl
|
||||||
|
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
catch ( ... )
|
||||||
|
{
|
||||||
|
std::cerr << std::endl
|
||||||
|
<< "unknown exception." << std::endl;
|
||||||
|
return 1;
|
||||||
|
}}
|
||||||
|
|
||||||
Reference in New Issue
Block a user