hello-algo/codes/cpp/include/TreeNode.hpp

67 lines
1.4 KiB
C++
Raw Normal View History

/*
* File: PrintUtil.hpp
* Created Time: 2021-12-19
* Author: Krahets (krahets@163.com)
*/
#pragma once
/**
* @brief Definition for a binary tree node
*
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
/**
* @brief Generate a binary tree with a vector
*
* @param list
* @return TreeNode*
*/
TreeNode *vecToTree(vector<int> list) {
if (list.empty()) {
return nullptr;
}
auto *root = new TreeNode(list[0]);
queue<TreeNode *> que;
size_t n = list.size(), index = 1;
while (index < n) {
auto node = que.front();
que.pop();
if (index < n) {
node->left = new TreeNode(list[index++]);
que.emplace(node->left);
}
if (index < n) {
node->right = new TreeNode(list[index++]);
que.emplace(node->right);
}
}
return root;
}
/**
* @brief Get a tree node with specific value in a binary tree
*
* @param root
* @param val
* @return TreeNode*
*/
TreeNode *getTreeNode(TreeNode *root, int val) {
if (root == nullptr)
return nullptr;
if (root->val == val)
return root;
TreeNode *left = getTreeNode(root->left, val);
TreeNode *right = getTreeNode(root->right, val);
return left != nullptr ? left : right;
}