/**
* 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* root=bd(nums,0,nums.size()-1);
return root;
}
TreeNode* bd(vector<int>& nums, int l, int r){
TreeNode* node=new TreeNode;
if(l<r){
int max=l;
for(int i=l;i<=r;i++){
if(nums[max]<nums[i]){
max=i;
}
}
node->val=nums[max];
if(max>l) node->left=bd(nums,l,max-1);
if(max<r) node->right=bd(nums,max+1,r);
}else{
node->val=nums[l];
}
return node;
}
};
搜索区间内的最大值,以最大值为分界点,得到两个新的区间,直到区间内只有一个元素。
递归
/**
* 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&&root2!=NULL) return root2;
if(root1!=NULL&&root2!=NULL){
root1->val+=root2->val;
root1->left=mergeTrees(root1->left,root2->left);
root1->right=mergeTrees(root1->right,root2->right);
}
return root1;
}
};
层序遍历
/**
* 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(root2==NULL) return root1;
if(root1==NULL) return root2;
queue<TreeNode*>q;
q.push(root1);
q.push(root2);
while(!q.empty()){
TreeNode* p1=q.front();
q.pop();
TreeNode* p2=q.front();
q.pop();
p1->val+=p2->val;
if(p1->left!=NULL&&p2->left!=NULL){
q.push(p1->left);
q.push(p2->left);
}else if(p1->left==NULL){
p1->left=p2->left;
}
if(p1->right!=NULL&&p2->right!=NULL){
q.push(p1->right);
q.push(p2->right);
}else if(p1->right==NULL){
p1->right=p2->right;
}
}
return root1;
}
};
对两颗树以相同的遍历顺序进行遍历,遇到树1中的空结点,直接将树2的同位置节点转移到树1上,遇到树2的空节点则不用处理。
/**
* 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) {
while(root!=NULL){
if(root->val>val){
root=root->left;
}else if(root->val<val){
root=root->right;
}
else{
return root;
}
}
return root;
}
};
比较简单。
/**
* 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:
bool isValidBST(TreeNode* root) {
stack<TreeNode*>s;
s.push(root);
long int pre=(long)INT_MIN-1;
while(!s.empty()){
if(s.top()==NULL){
s.pop();
if(pre<s.top()->val){
pre=s.top()->val;
s.pop();
}else{
return false;
}
}else{
TreeNode* node=s.top();
s.pop();
if(node->right!=NULL) s.push(node->right);
s.push(node);
s.push(NULL);
if(node->left!=NULL) s.push(node->left);
}
}
return true;
}
};
这题的诀窍在于搜索树在中序遍历中是一个单增数列,因此只要验证树在中序遍历中是严格递增的即可。