-
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.
Create make_tree for the fixed binary tree of the example.
- Loading branch information
Showing
2 changed files
with
34 additions
and
16 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 |
---|---|---|
@@ -1,9 +1,37 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <utility> | ||
#include <vector> | ||
|
||
using std::pair; | ||
using std::make_unique; | ||
using std::unique_ptr; | ||
using std::vector; | ||
|
||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
}; | ||
|
||
pair<unique_ptr<TreeNode>, vector<unique_ptr<TreeNode>>> | ||
make_tree(const vector<int>& v) { | ||
auto root{std::make_unique<TreeNode>(v.at(0))}; | ||
|
||
// FIXME const construction. | ||
vector<unique_ptr<TreeNode>> otherNodes; | ||
//TODO use actual data in v to construct the tree. | ||
otherNodes.push_back(make_unique<TreeNode>(9)); | ||
otherNodes.push_back(make_unique<TreeNode>(20)); | ||
otherNodes.push_back(make_unique<TreeNode>(15)); | ||
otherNodes.push_back(make_unique<TreeNode>(7)); | ||
|
||
root->left = otherNodes[0].get(); | ||
root->right = otherNodes[1].get(); | ||
root->right->left = otherNodes[2].get(); | ||
root->right->right = otherNodes[3].get(); | ||
|
||
return std::make_pair(std::move(root), std::move(otherNodes)); | ||
} |