代码随想录算法训练营第十八天 | 530.二叉搜索树的最小绝对差, 501.二叉搜索树中的众数 & 236. 二叉树的最近公共祖先
530. Minimum Absolute Difference in BST
思路:这题一开始是有一些些思路的,但是没想清楚为什么要用中序遍历,之后看了视频询问了过后就非常清晰了,中序遍历能保证数字由小到大排列,那么最小差值一定是在相邻俩数间产生的
题目链接:530. Minimum Absolute Difference in BST
文章讲解:二叉搜索树的最小绝对差
视频讲解:二叉搜索树的最小绝对差
我的代码:
Python:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.result = float('inf')
self.pre = None
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
def traversal(root):
if not root:
return
traversal(root.left)
if (self.pre != None):
self.result = min(root.val - self.pre.val, self.result)
self.pre = root
traversal(root.right)
traversal(root)
return self.result
C++:
/**
* 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:
int result = INT_MAX;
TreeNode* pre = NULL;
void traversal(TreeNode* root) {
if (!root) {
return;
}
traversal(root->left);
if (pre != NULL) {
result = std::min(root->val - pre->val, result);
}
pre = root;
traversal(root->right);
}
public:
int getMinimumDifference(TreeNode* root) {
traversal(root);
return result;
}
};
501. Find Mode in Binary Search Tree
思路:写完上一题530之后这一题思路其实很清晰,就连需要的小技巧(如何track最大的count,何时需要更新result数组)都想到了,但居然没想到每一个数的count在哪里记录,看了讲解之后才知道也可以放在global变量里头
题目链接:501. Find Mode in Binary Search Tree
文章讲解:二叉搜索树中的众数
视频讲解:二叉搜索树中的众数
我的代码:
Python:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.count = 0
self.max_count = 0
self.pre = None
self.result = []
def findMode(self, root: Optional[TreeNode]) -> List[int]:
def traversal(root):
if not root:
return
traversal(root.left)
if (not self.pre):
self.count = 1
elif self.pre.val == root.val:
self.count += 1
else:
self.count = 1
if self.count == self.max_count:
self.result.append(root.val)
if self.count > self.max_count:
self.max_count = self.count
self.result.clear()
self.result.append(root.val)
self.pre = root
traversal(root.right)
traversal(root)
return self.result
C++:
/**
* 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* prev_{NULL};
int max_count_ = 0;
int cur_count_ = 1;
vector<int> result;
void traversal(TreeNode* node) {
if (!node) {
return;
}
traversal(node->left);
if (prev_ == NULL) {
cur_count_ = 1;
} else if (prev_ != NULL && node->val == prev_->val) {
cur_count_++;
} else {
cur_count_ = 1;
}
if (cur_count_ == max_count_) {
result.push_back(node->val);
}
if (cur_count_ > max_count_) {
max_count_ = cur_count_;
result.clear();
result.push_back(node->val);
}
prev_ = node;
traversal(node->right);
}
public:
vector<int> findMode(TreeNode* root) {
traversal(root);
return result;
}
};
236. Lowest Common Ancestor of a Binary Tree
思路:这题直接看的视频,感觉讲解的非常清晰,看完视频后写代码很顺畅
题目链接: 236. Lowest Common Ancestor of a Binary Tree
文章讲解:二叉树的最近公共祖先
视频讲解:二叉树的最近公共祖先
我的代码:
C++:
/**
* 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 || root == p || root == q) {
return root;
}
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if (left != NULL && right != NULL) {
return root;
} else if (left == NULL && right != NULL) {
return right;
} else {
return left;
}
}
};
Python:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if not root or root == p or root == q:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if left != None and right != None:
return root
elif left == None and right != None:
return right
else:
return left
总结
还是收获满满的。但是530&501居然是Easy,哭了,还是要好好练习,然后加油赶进度啊,欠了四周的但我还是感觉有希望追上的5555