方法:递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
TreeNode* solve(TreeNode* cur, TreeNode* p, TreeNode* q) {
if (cur == NULL) return cur;
if (cur->val > p->val && cur->val > q->val) {
TreeNode* left = solve(cur->left, p, q);
if (left != NULL) return left;
}
if (cur->val < p->val && cur->val < q->val) {
TreeNode* right = solve(cur->right, p, q);
if (right != NULL) return right;
}
return cur;
}
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
return solve(root, p, q);
}
};
$时间复杂度O(n),空间复杂度O(n)
方法:迭代
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
while (root) {
if (root->val > p->val && root->val > q->val) root = root->left;
else if (root->val < p->val && root->val < q->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:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == NULL) return new TreeNode(val);
if (val > root->val) root->right = insertIntoBST(root->right, val);
else if (val < root->val) root->left = insertIntoBST(root->left, 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 {
private:
TreeNode* pre;
void solve(TreeNode* cur, int val) {
if (cur == NULL) {
TreeNode* nod = new TreeNode(val);
if (val > pre->val) pre->right = nod;
else pre->left = nod;
return ;
}
pre = cur;
if (val < cur->val) solve(cur->left, val);
if (val > cur->val) solve(cur->right, val);
return ;
}
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
pre = new TreeNode(0);
if (root == NULL) {
root = new TreeNode(val);
}
solve(root, 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* insertIntoBST(TreeNode* root, int val) {
if (root == NULL) {
TreeNode* nod = new TreeNode(val);
return nod;
}
TreeNode* cur = root;
TreeNode* pre = root;
while (cur != NULL) {
pre = cur;
if (cur->val > val) cur = cur->left;
else cur = cur->right;
}
TreeNode* nod = new TreeNode(val);
if (pre->val > val) pre->left = nod;
else pre->right = nod;
return root;
}
};
$时间复杂度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:
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == NULL) return root;
if (root->val == key) {
if (!root->left && !root->right) {
delete root;
return NULL;
} else if (root->left == NULL) {
auto r = root->right;
delete root;
return r;
} else if (!root->right) {
auto l = root->left;
delete root;
return l;
} else {
TreeNode* cur = root->right;
while (cur->left != NULL) cur = cur->left;
cur->left = root->left;
TreeNode* tmp = root;
root = root->right;
delete tmp;
return root;
}
}
if (root->val > key) root->left = deleteNode(root->left, key);
if (root->val < key) root->right = deleteNode(root->right, key);
return root;
}
};
$时间复杂度O(n),空间复杂度O(n)