mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-24 02:03:10 +08:00
Add C++ code for the chapter binary tree.
This commit is contained in:
@ -25,7 +25,7 @@ struct ListNode {
|
||||
* @param list
|
||||
* @return ListNode*
|
||||
*/
|
||||
ListNode* vectorToLinkedList(vector<int>& list) {
|
||||
ListNode* vecToLinkedList(vector<int> list) {
|
||||
ListNode *dum = new ListNode(0);
|
||||
ListNode *head = dum;
|
||||
for (int val : list) {
|
||||
|
@ -238,4 +238,27 @@ class PrintUtil {
|
||||
}
|
||||
cout << "[" + s.str() + "]" << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* @tparam T
|
||||
* @param queue
|
||||
*/
|
||||
template <typename T>
|
||||
static void printQueue(queue<T> &queue)
|
||||
{
|
||||
// Generate the string to print
|
||||
ostringstream s;
|
||||
bool flag = true;
|
||||
while(!queue.empty()) {
|
||||
if (flag) {
|
||||
s << queue.front();
|
||||
flag = false;
|
||||
}
|
||||
else s << ", " << queue.front();
|
||||
queue.pop();
|
||||
}
|
||||
cout << "[" + s.str() + "]" << '\n';
|
||||
}
|
||||
};
|
||||
|
@ -23,7 +23,7 @@ struct TreeNode {
|
||||
* @param list
|
||||
* @return TreeNode*
|
||||
*/
|
||||
TreeNode* vectorToTree(vector<int>& list) {
|
||||
TreeNode* vecToTree(vector<int> list) {
|
||||
TreeNode *root = new TreeNode(list[0]);
|
||||
queue<TreeNode*> que;
|
||||
que.emplace(root);
|
||||
|
Reference in New Issue
Block a user