方法:递归
/**
* 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:
int getMinimumDifference(TreeNode* root) {
solve(root);
int n = res.size(), minv = 1e5+10;
for (int i = 1; i < n; ++i) {
if (res[i] - res[i-1] < minv) minv = res[i] - res[i-1];
}
return minv;
}
};
$时间复杂度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:
int getMinimumDifference(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* pre = NULL;
int minv = 1e5+10;
while (cur != NULL || !st.empty()) {
if (cur != NULL) {
st.push(cur);
cur = cur->left;
} else {
cur = st.top();
st.pop();
if (pre != NULL) {
minv = min(minv, cur->val - pre->val);
}
pre = cur;
cur = cur->right;
}
}
return minv;
}
};
$时间复杂度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;
TreeNode* pre = NULL;
int maxv = 0, cnt = 0;
void solve(TreeNode* cur) {
if (cur == NULL) return ;
solve(cur->left);
if (pre == NULL) cnt = 1;
else if (cur->val == pre->val) ++cnt;
else cnt = 1;
if (cnt == maxv) res.push_back(cur->val);
if (cnt > maxv) {
res.clear();
maxv = cnt;
res.push_back(cur->val);
}
pre = cur;
solve(cur->right);
return ;
}
public:
vector<int> findMode(TreeNode* root) {
solve(root);
return res;
}
};
$时间复杂度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:
vector<int> findMode(TreeNode* root) {
int cnt = 0, maxv = 0;
stack<TreeNode*> stk;
TreeNode* cur = root;
TreeNode* pre = NULL;
vector<int> res;
while (cur != NULL || !stk.empty()) {
if (cur != NULL) {
stk.push(cur);
cur = cur->left;
} else {
cur = stk.top();
stk.pop();
if (pre == NULL) cnt = 1;
else if (pre->val == cur->val) ++cnt;
else cnt = 1;
if (cnt == maxv) res.push_back(cur->val);
if (cnt > maxv) {
maxv = cnt;
res.clear();
res.push_back(cur->val);
}
pre = cur;
cur = cur->right;
}
}
return res;
}
};
$时间复杂度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) {
if (root == p || root == q || root == NULL) return root;
TreeNode* l = lowestCommonAncestor(root->left, p, q);
TreeNode* r = lowestCommonAncestor(root->right, p, q);
if (l != NULL && r != NULL) return root;
else if (l == NULL && r != NULL) return r;
else return l;
}
};
$时间复杂度O(n),空间复杂度O(n)