方法:递归
/**
* 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 {
private:
TreeNode* solve(vector<int>& nums, int l, int r) {
if (l >= r) return nullptr;
int maxv = l;
for (int i = l + 1; i < r; ++i) if (nums[i] > nums[maxv]) maxv = i;
TreeNode* root = new TreeNode(nums[maxv]);
root->left = solve(nums, l, maxv);
root->right = solve(nums, maxv + 1, r);
return root;
}
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return solve(nums, 0, nums.size());
}
};
$时间复杂度O(),时间复杂度O(n);
方法:递归
/**
* 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) return root2;
if (!root2) return root1;
root1->left = mergeTrees(root1->left, root2->left);
root1->val += root2->val;
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
$时间复杂度O(min(n, m)),时间复杂度O(min(n, m));
方法:BFS
/**
* 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) return root2;
if (!root2) return root1;
queue<TreeNode*> q;
q.push(root1);
q.push(root2);
while (!q.empty()) {
TreeNode* nod1 = q.front(); q.pop();
TreeNode* nod2 = q.front(); q.pop();
nod1->val += nod2->val;
if (nod1->left && nod2->left) {
q.push(nod1->left);
q.push(nod2->left);
}
if (nod1->right && nod2->right) {
q.push(nod1->right);
q.push(nod2->right);
}
if (nod1->left == NULL && nod2->left != NULL) nod1->left = nod2->left;
if (nod1->right == NULL && nod2->right != NULL) nod1->right = nod2->right;
}
return root1;
}
};
$时间复杂度O(min(n, m)),时间复杂度O(min(n, m));
方法:递归
/**
* 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) return NULL;
if (root->val > val) return searchBST(root->left, val);
else if (root->val < val) return searchBST(root->right, val);
return root;
}
};
$时间复杂度O(n),时间复杂度O(n);
方法:迭代
/**
* 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) {
if (root->val > val) root = root->left;
else if (root->val < val) root = root->right;
else return root;
}
return NULL;
}
};
$时间复杂度O(n),时间复杂度O(1);
方法:迭代
/**
* 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*> st;
TreeNode* cur = root;
TreeNode* pre = NULL;
while (!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;
}
};
$时间复杂度O(n),时间复杂度O(n);
方法:递归
/**
* 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 {
private:
vector<int> res;
void solve(TreeNode* cur) {
if (cur == NULL) return ;
solve(cur->left);
res.push_back(cur->val);
solve(cur->right);
}
public:
bool isValidBST(TreeNode* root) {
solve(root);
for (int i = 1; i < res.size(); ++i) if (res[i] <= res[i-1]) return false;
return true;
}
};
$时间复杂度O(n),时间复杂度O(n);