LeetCode654.最大二叉树
题目链接:654. 最大二叉树 - 力扣(LeetCode)
思路:
凡是构造二叉树的都用前序遍历
构造树一般采用的是前序遍历,因为先构造中间节点,然后递归构造左子树和右子树。
/**
* Definition for a binary tree node.
* 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:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
TreeNode* node = new TreeNode(0);
if(nums.size() == 1) {
node->val = nums[0];
return node;
}
//找到数组中最大的值和对应的下标
int maxvalue = 0;
int MAXV = 0;
for(int i = 0;i < nums.size();i++) {
if(nums[i] > maxvalue) {
maxvalue = nums[i];
MAXV = i;
}
}
node->val = maxvalue;
//最大值所在下标左区间,构造左子树
if(MAXV > 0) {
vector<int> newVec(nums.begin(),nums.begin() + MAXV);
node->left = constructMaximumBinaryTree(newVec);
}
//最大值所在下标右区间,构造右子树
if(MAXV < (nums.size() - 1)) {
vector<int> newVec1(nums.begin() + MAXV + 1,nums.end());
node->right = constructMaximumBinaryTree(newVec1);
}
return node;
}
};
LeetCode617.合并二叉树
题目链接:617. 合并二叉树 - 力扣(LeetCode)
思路:
/**
* Definition for a binary tree node.
* 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:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1 == NULL) return root2;//t1为空,合并后为t2
if(root2 == NULL) return root1;//t2为空,合并后为t1
root1->val += root2->val;//如果都不为空,值相加
root1->left = mergeTrees(root1->left,root2->left);
root1->right = mergeTrees(root1->right,root2->right);
return root1;
}
};
LeetCode700.二叉搜索树中的搜索
题目链接:700. 二叉搜索树中的搜索 - 力扣(LeetCode)
思路:
二叉搜索树是一个有序树(多利用特性):
- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
- 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
- 它的左、右子树也分别为二叉搜索树。
/**
* Definition for a binary tree node.
* 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:
TreeNode* searchBST(TreeNode* root, int val) {
if(root == NULL || root->val == val) return root;
TreeNode* result = NULL;
if(val < root->val) result = searchBST(root->left,val);
else result = searchBST(root->right,val);
return result;
}
};
LeetCode98.验证二叉搜索树
题目链接:98. 验证二叉搜索树 - 力扣(LeetCode)
思路:
中序遍历下,输出的二叉搜索树节点的数值是有序序列。
有了这个特性,验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。
/**
* Definition for a binary tree node.
* 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:
long long maxval = LONG_MIN;
bool isValidBST(TreeNode* root) {
if(root == NULL) return true;
bool left = isValidBST(root->left);//左
if(root->val > maxval) maxval = root->val;//中
else return false;
bool right = isValidBST(root->right);//右
return left && right;
}
};