-
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
a501419
commit 5be8731
Showing
3 changed files
with
58 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
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,33 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <gtest/gtest.h> | ||
|
||
struct TreeNode { | ||
const int32_t value; | ||
TreeNode *left; | ||
TreeNode *right; | ||
explicit TreeNode(int32_t value) : | ||
value(value), left(nullptr), right(nullptr) { } | ||
}; | ||
|
||
class Solution { | ||
public: | ||
static auto count_nodes(TreeNode *root) -> int32_t { | ||
if (root == nullptr) return 0; | ||
|
||
int32_t count = 0; | ||
std::function<void(TreeNode*)> preorder = | ||
[&](TreeNode *root) -> void { | ||
if (root == nullptr) return; | ||
++count; | ||
preorder(root->left); | ||
preorder(root->right); | ||
}; | ||
|
||
preorder(root); | ||
return count; | ||
} | ||
}; |
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,14 @@ | ||
#include "../include/solution.hpp" | ||
|
||
auto main() -> int { | ||
auto *root = new TreeNode(1); | ||
root->left = new TreeNode(2); | ||
root->left->left = new TreeNode(4); | ||
root->left->right = new TreeNode(5); | ||
root->right = new TreeNode(3); | ||
root->right->left = new TreeNode(6); | ||
|
||
std::cout << Solution::count_nodes(root); | ||
|
||
return 0; | ||
} |