654. 最大二叉树
题目
题解
注意
1.遍历顺序:
前序遍历:
中:寻找最大值,创建二叉树节点
左:遍历二叉树
右:遍历二叉树
2.求vector中的最大/小值索引函数
std::max_element() 函数的语法
std::max_element(iterator start, iterator end, [compare comp]);
参数:
iterator start, iterator end- 这些是指向容器中范围的迭代器位置。
[compare comp]- 它是一个可选参数(一个函数),用于与给定范围内的元素进行比较。
返回值: iterator- 它返回一个迭代器,指向给定范围内具有最大值的元素。
例:
Input:
int arr[] = { 100, 200, -100, 300, 400 };
//finding largest element
int result = *max_element(arr + 0, arr + 5);
cout << result << endl;
Output:
400
int a[6] = {5, 3, 2, 6, 1, 4};
int b = a[0];
int c = a[1];
cout<<max(b, c)<<" "<<min(b,c)<<endl; //输出为5 3
cout<<max_element(a, a+6) - a<<endl;// 输出为3
cout<<*max_element(a, a+6)<<endl;//输出为 6
cout<<min_element(a, a+6) - a<<endl;// 输出为4
cout<<*min_element(a, a+6)<<endl; //输出为1
617. 合并二叉树
题目
题解
递归
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if (t1 == NULL) return t2; // 如果t1为空,合并之后就应该是t2
if (t2 == NULL) return t1; // 如果t2为空,合并之后就应该是t1
// 修改了t1的数值和结构
t1->val += t2->val; // 中
t1->left = mergeTrees(t1->left, t2->left); // 左
t1->right = mergeTrees(t1->right, t2->right); // 右
return t1;
}
};
迭代
对两棵树进行遍历,遍历结果放在一棵树中
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
TreeNode* node = new TreeNode(0);
if (nums.size() == 1) {
node->val = nums[0];
return node;
}
// 找到数组中最大的值和对应的下标
int maxValue = 0;
int maxValueIndex = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] > maxValue) {
maxValue = nums[i];
maxValueIndex = i;
}
}
node->val = maxValue;
// 最大值所在的下标左区间 构造左子树
if (maxValueIndex > 0) {
vector<int> newVec(nums.begin(), nums.begin() + maxValueIndex);
node->left = constructMaximumBinaryTree(newVec);
}
// 最大值所在的下标右区间 构造右子树
if (maxValueIndex < (nums.size() - 1)) {
vector<int> newVec(nums.begin() + maxValueIndex + 1, nums.end());
node->right = constructMaximumBinaryTree(newVec);
}
return node;
}
};
注意
1.遍历顺序
前序遍历:
中:叠加
左:遍历
右:遍历
700. 二叉搜索树中的搜索
题目
题解
递归
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (root == NULL || root->val == val) return root;
TreeNode* result = NULL;
if (root->val > val) result = searchBST(root->left, val);
if (root->val < val) result = searchBST(root->right, val);
return result;
}
};
迭代
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while (root != NULL) {
if (root->val > val) root = root->left;
else if (root->val < val) root = root->right;
else return root;
}
return NULL;
}
};
98. 验证二叉搜索树
题目
题解
递归
class Solution {
public:
long long maxVal = LONG_MIN; // 因为后台测试数据中有int最小值
bool isValidBST(TreeNode* root) {
if (root == NULL) return true;
bool left = isValidBST(root->left);
// 中序遍历,验证遍历的元素是不是从小到大
if (maxVal < root->val) maxVal = root->val;
else return false;
bool right = isValidBST(root->right);
return left && right;
}
};
迭代
class Solution {
public:
bool isValidBST(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* pre = NULL; // 记录前一个节点
while (cur != NULL || !st.empty()) {
if (cur != NULL) {
st.push(cur);
cur = cur->left; // 左
} else {
cur = st.top(); // 中
st.pop();
if (pre != NULL && cur->val <= pre->val)
return false;
pre = cur; //保存前一个访问的结点
cur = cur->right; // 右
}
}
return true;
}
};
注意
1.中序遍历
左:遍历
中:判断是否为二叉搜索树,若是,则记录二叉搜索树的节点值
右:遍历
2.中序遍历下,输出的二叉搜索树节点的数值是有序序列。
3.空二叉树既是完全二叉树也是搜索二叉树也是满二叉树。
4.不能单纯的比较左节点小于中间节点,右节点大于中间节点
反例:
5.样例中最小节点 可能是int的最小值,如果这样使用最小的int来比较也是不行的。此时可以初始化比较元素为longlong的最小值。