class Solution {
public:
TreeNode* buildTree(vector& inorder, vector& postorder) {
if(postorder.size() == 0) return NULL;
int rootVal = postorder[postorder.size()-1];
TreeNode* root = new TreeNode(rootVal);
if(postorder.size() == 1) return root;
int delimiterIndex;
for(delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++){
if(inorder[delimiterIndex] == rootVal){
break;
}
}
vector leftinorder(inorder.begin(), inorder.begin()+delimiterIndex);
vector rightinorder(inorder.begin()+delimiterIndex+1, inorder.end());
postorder.resize(postorder.size()-1);
vector leftpostorder(postorder.begin(), postorder.begin()+leftinorder.size());
vector rightpostorder(postorder.begin()+leftinorder.size(), postorder.end());
root->left = buildTree(leftinorder, leftpostorder);
root->right = buildTree(rightinorder, rightpostorder);
return root;
}
};
直接用下标写
class Solution {
public:
TreeNode* buildTree(vector& inorder, vector& postorder) {
if(inorder.size() == 0) return NULL;
return traversal(inorder, 0, inorder.size(), postorder, 0, postorder.size());
}
private:
TreeNode* traversal(vector& inorder, int inorderBegin, int inorderEnd, vector& postorder, int postorderBegin, int postorderEnd){
if(postorderBegin == postorderEnd) return NULL;
int rootVal = postorder[postorderEnd-1];
TreeNode* root = new TreeNode(rootVal);
if(postorderEnd - postorderBegin == 1) return root;
int delimiterIndex;
for(delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++){
if(inorder[delimiterIndex] == rootVal){
break;
}
}
int leftInorderBegin = inorderBegin;
int leftInorderEnd = delimiterIndex;
int rightInorderBegin = delimiterIndex + 1;
int rightInorderEnd = inorderEnd;
int leftPostorderBegin = postorderBegin;
int leftPostorderEnd = postorderBegin + delimiterIndex - inorderBegin;
int rightPostorderBegin = postorderBegin + delimiterIndex - inorderBegin;
int rightPostorderEnd = postorderEnd - 1;
root->left = traversal(inorder, leftInorderBegin, leftInorderEnd, postorder, leftPostorderBegin, leftPostorderEnd);
root->right = traversal(inorder, rightInorderBegin, rightInorderEnd, postorder, rightPostorderBegin, rightPostorderEnd);
return root;
}
};
class Solution {
public:
TreeNode* buildTree(vector& preorder, vector& inorder) {
if(preorder.size() == 0) return NULL;
return traversal(preorder, 0, preorder.size(), inorder, 0, inorder.size());
}
TreeNode* traversal(vector& preorder, int preorderBegin, int preorderEnd, vector inorder, int inorderBegin, int inorderEnd){
if(preorderBegin - preorderEnd == 0) return NULL;
int rootVal = preorder[preorderBegin];
TreeNode* root = new TreeNode(rootVal);
if(preorderEnd - preorderBegin == 1) return root;
int delimiterIndex;
for(delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++){
if(inorder[delimiterIndex] == rootVal) break;
}
int leftInorderBegin = inorderBegin;
int leftInorderEnd = delimiterIndex;
int rightInderBegin = delimiterIndex+1;
int rightInderEnd = inorderEnd;
int leftPreorderBegin = preorderBegin + 1;
int leftPreorderEnd = leftPreorderBegin + leftInorderEnd - leftInorderBegin;
int rightPreorderBegin = leftPreorderEnd;
int rightPreorderEnd = preorderEnd;
root->left = traversal(preorder, leftPreorderBegin, leftPreorderEnd, inorder, leftInorderBegin, leftInorderEnd);
root->right = traversal(preorder, rightPreorderBegin, rightPreorderEnd, inorder, rightInderBegin, rightInderEnd);
return root;
}
}
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector& nums) {
return traversal(nums, 0, nums.size());
}
TreeNode* traversal(vector& nums, int begin, int end){
if(end-begin == 0) return NULL;
int maxVal = INT_MIN;
int maxIndex;
for(int i=begin; ileft = traversal(nums, leftBegin, leftEnd);
root->right = traversal(nums, rightBegin, rightEnd);
return root;
}
};
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(!root1) return root2;
if(!root2) return root1;
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root == NULL) return NULL;
if(root->val == val) return root;
if(root->val > val) return searchBST(root->left, val);
if(root->val < val) return searchBST(root->right, val);
return NULL;
}
};
要有root == NULL的终止条件
最后要有return NULL,因为没有写else, 所以默认会有其他情况
如果不写return NULL也可以这么写
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root == NULL) return NULL;
if(root->val == val) return root;
if(root->val > val) return searchBST(root->left, val);
else return searchBST(root->right, val);
}
};
2.迭代法
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while(root != NULL){
if(root->val == val) return root;
else if(root->val > val) root = root->left;
else root = root->right;
}
return NULL;
}
};
两个方法思想一样,一个先提取出生成一个数组, 一个直接递归
class Solution {
private:
vector res;
void traversal(TreeNode* root){
if(root == NULL) return;
traversal(root->left);
res.push_back(root->val);
traversal(root->right);
}
public:
bool isValidBST(TreeNode* root) {
res.clear();
traversal(root);
for(int i=1; i
class Solution {
public:
TreeNode* pre = NULL;
bool isValidBST(TreeNode* root) {
if(root == NULL) return true;
bool left = isValidBST(root->left);
if(pre != NULL && pre->val >= root->val) return false;
pre = root;
bool right = isValidBST(root->right);
return left && right;
}
};
跟前一道题的两种想法比较像
class Solution {
private:
vector res;
void traversal(TreeNode* root){
if(root == NULL) return;
traversal(root->left);
res.push_back(root->val);
traversal(root->right);
}
public:
int getMinimumDifference(TreeNode* root) {
res.clear();
traversal(root);
int minDif = INT_MAX;
for(int i=1; i
class Solution {
private:
TreeNode* pre = NULL;
int minDif = INT_MAX;
void traversal(TreeNode* root){
if(root == NULL) return;
traversal(root->left);
if(pre != NULL){
int dif = root->val - pre->val;
if(dif < minDif) minDif = dif;
}
pre = root;
traversal(root->right);
}
public:
int getMinimumDifference(TreeNode* root) {
traversal(root);
return minDif;
}
};