106. Construct Binary Tree from Inorder and Postorder Traversal
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& 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<int> leftinorder(inorder.begin(), inorder.begin()+delimiterIndex);
vector<int> rightinorder(inorder.begin()+delimiterIndex+1, inorder.end());
postorder.resize(postorder.size()-1);
vector<int> leftpostorder(postorder.begin(), postorder.begin()+leftinorder.size());
vector<int> 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<int>& inorder, vector<int>& postorder) {
if(inorder.size() == 0) return NULL;
return traversal(inorder, 0, inorder.size(), postorder, 0, postorder.size());
}
private:
TreeNode* traversal(vector<int>& inorder, int inorderBegin, int inorderEnd, vector<int>& 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;
}
};
105. Construct Binary Tree from Preorder and Inorder Traversal
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.size() == 0) return NULL;
return traversal(preorder, 0, preorder.size(), inorder, 0, inorder.size());
}
TreeNode* traversal(vector<int>& preorder, int preorderBegin, int preorderEnd, vector<int> 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;
}
}
654. Maximum Binary Tree
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return traversal(nums, 0, nums.size());
}
TreeNode* traversal(vector<int>& nums, int begin, int end){
if(end-begin == 0) return NULL;
int maxVal = INT_MIN;
int maxIndex;
for(int i=begin; i<end; i++){
if(maxVal < nums[i]){
maxVal = nums[i];
maxIndex = i;
}
}
TreeNode* root = new TreeNode(maxVal);
if(end-begin == 1) return root;
int leftBegin = begin;
int leftEnd = maxIndex;
int rightBegin = maxIndex + 1;
int rightEnd = end;
root->left = traversal(nums, leftBegin, leftEnd);
root->right = traversal(nums, rightBegin, rightEnd);
return root;
}
};
617. Merge Two Binary Trees
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;
}
};
700. Search in a Binary Search Tree
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;
}
};
98. Validate Binary Search Tree
两个方法思想一样,一个先提取出生成一个数组, 一个直接递归
class Solution {
private:
vector<int> 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<res.size(); i++){
if(res[i] <= res[i-1]) return false;
}
return true;
}
};
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;
}
};
530. Minimum Absolute Difference in BST
跟前一道题的两种想法比较像
class Solution {
private:
vector<int> 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<res.size(); i++){
int dif = res[i] - res[i-1];
if(dif < minDif) minDif = dif;
}
return minDif;
}
};
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;
}
};