文章目录
- 一、题目
- 二、解法
- 三、完整代码
所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。
一、题目
二、解法
思路分析:二叉搜索树的性质:左节点键值 < 中间节点键值 < 右节点键值。那么我们根据此性质,对比目标值和中间节点,如果val值较小在左边子树进行搜索,否则在右边子树进行搜索。程序采用递归实现。
程序如下:
class Solution {
public:// 递归法// 1、输入参数TreeNode* searchBST(TreeNode* root, int val) { // 2.终止条件if (root == NULL || root->val == val) return root; // 找到目标值,标志位变为1,返回目标节点// 3.单层递归逻辑:对比根节点和val if (root->val > val) return searchBST(root->left, val); // val较小,在左边子树 if (root->val < val) return searchBST(root->right, val); // val较大,在右边子树// 1.返回值return NULL;}
};
复杂度分析:
- 时间复杂度: O ( n ) O(n) O(n)。
- 空间复杂度: O ( n ) O(n) O(n)。
三、完整代码
# include <iostream>
# include <vector>
# include <string>
# include <queue>
# include <stack>
using namespace std;// 树节点定义
struct TreeNode {int val;TreeNode* left;TreeNode* right;TreeNode() : val(0), left(nullptr), right(nullptr) {}TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};class Solution {
public:// 递归法// 1、输入参数TreeNode* searchBST(TreeNode* root, int val) { // 2.终止条件if (root == NULL || root->val == val) return root; // 找到目标值,标志位变为1,返回目标节点// 3.单层递归逻辑:对比根节点和val if (root->val > val) return searchBST(root->left, val); // val较小,在左边子树 if (root->val < val) return searchBST(root->right, val); // val较大,在右边子树// 1.返回值return NULL;}
};// 前序遍历迭代法创建二叉树,每次迭代将容器首元素弹出(弹出代码还可以再优化)
void Tree_Generator(vector<string>& t, TreeNode*& node) {if (!t.size() || t[0] == "NULL") return; // 退出条件else {node = new TreeNode(stoi(t[0].c_str())); // 中if (t.size()) {t.assign(t.begin() + 1, t.end());Tree_Generator(t, node->left); // 左}if (t.size()) {t.assign(t.begin() + 1, t.end());Tree_Generator(t, node->right); // 右}}
}template<typename T>
void my_print(T& v, const string msg)
{cout << msg << endl;for (class T::iterator it = v.begin(); it != v.end(); it++) {cout << *it << ' ';}cout << endl;
}template<class T1, class T2>
void my_print2(T1& v, const string str) {cout << str << endl;for (class T1::iterator vit = v.begin(); vit < v.end(); ++vit) {for (class T2::iterator it = (*vit).begin(); it < (*vit).end(); ++it) {cout << *it << ' ';}cout << endl;}
}// 层序遍历
vector<vector<int>> levelOrder(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);vector<vector<int>> result;while (!que.empty()) {int size = que.size(); // size必须固定, que.size()是不断变化的vector<int> vec;for (int i = 0; i < size; ++i) {TreeNode* node = que.front();que.pop();vec.push_back(node->val);if (node->left) que.push(node->left);if (node->right) que.push(node->right);}result.push_back(vec);}return result;
}int main()
{vector<string> t1 = { "4", "2", "1", "NULL", "NULL", "3", "NULL", "NULL", "7", "NULL", "NULL" }; // 前序遍历my_print(t1, "目标树");TreeNode* root1 = new TreeNode();Tree_Generator(t1, root1);vector<vector<int>> tree1 = levelOrder(root1);my_print2<vector<vector<int>>, vector<int>>(tree1, "目标树:");Solution s;int val = 2;TreeNode* root = s.searchBST(root1, val);vector<vector<int>> tree = levelOrder(root);my_print2<vector<vector<int>>, vector<int>>(tree, "目标树:");system("pause");return 0;
}
end