给你一个二叉搜索树的根节点 root
,返回 树中任意两不同节点值之间的最小差值 。
差值是一个正数,其数值等于两值之差的绝对值。
示例 1:
输入:root = [4,2,6,1,3]
输出:1
示例 2:
输入:root = [1,0,48,null,null,12,49]
输出:1
提示:
[2, 104]
0 <= Node.val <= 105
/**
* 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 result = INT_MAX;
TreeNode * pre = nullptr;
void traversal(TreeNode* root) {
if (root == nullptr){
return;
}
traversal(root->left);
if (pre != nullptr) {
result = min(result, abs(root->val - pre->val));
}
// 记录前一个节点
pre = root;
traversal(root->right);
}
int getMinimumDifference(TreeNode* root) {
traversal(root);
return result;
}
};
# 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 traversal(self, root):
if not root:
return
self.traversal(root.left)
if self.pre != None:
self.result = min(self.result, abs(root.val - self.pre.val))
self.pre = root
self.traversal(root.right)
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
self.traversal(root)
return self.result
给你一个含重复值的二叉搜索树(BST)的根节点 root
,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。
如果树中有不止一个众数,可以按 任意顺序 返回。
假定 BST 满足如下定义:
示例 1:
输入:root = [1,null,2,2]
输出:[2]
示例 2:
输入:root = [0]
输出:[0]
提示:
[1, 104]
内-105 <= Node.val <= 105
**进阶:**你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)
/**
* 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> results;
int maxCount = 0;
int count = 0;
TreeNode * pre = nullptr;
void searchBST(TreeNode * root) {
if (root == nullptr) return;
// 左
searchBST(root->left);
// 中
// 判断前节点的值是否需要更新 count
if (pre == nullptr) {
count = 1;
} else if (pre != nullptr && root->val == pre->val) {
count++;
}else{
count = 1;
}
pre = root;
// 判断是否需要更新result。
if (count == maxCount) {
results.push_back(root->val);
}
if (count > maxCount) {
maxCount = count;
results.clear();
results.push_back(root->val);
}
// 右
searchBST(root->right);
}
vector<int> findMode(TreeNode* root) {
searchBST(root);
return results;
}
};
# 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.results = []
self.preNode = None
self.maxCount = 0
self.count = 0
def searchBST(self, root):
if not root:
return
self.searchBST(root.left)
if not self.preNode:
self.count = 1
elif self.preNode.val == root.val:
self.count += 1
else:
self.count = 1
self.preNode = root
if self.count == self.maxCount:
self.results.append(root.val)
if self.count > self.maxCount:
self.maxCount = self.count
self.results = []
self.results.append(root.val)
self.searchBST(root.right)
def findMode(self, root: Optional[TreeNode]) -> List[int]:
self.searchBST(root)
return self.results
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
示例 1:
输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出:3
解释:节点 5 和节点 1 的最近公共祖先是节点 3 。
示例 2:
输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出:5
解释:节点 5 和节点 4 的最近公共祖先是节点 5 。因为根据定义最近公共祖先节点可以为节点本身。
示例 3:
输入:root = [1,2], p = 1, q = 2
输出:1
提示:
[2, 105]
内。-109 <= Node.val <= 109
Node.val
互不相同
。p != q
p
和 q
均存在于给定的二叉树中。/**
* 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 == nullptr) return root;
TreeNode * left = lowestCommonAncestor(root->left, p, q);
TreeNode * right = lowestCommonAncestor(root->right, p, q);
if (left != nullptr && right != nullptr) return root;
if (left == nullptr) return right;
return left;
}
};
# 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 root == p or root == q or root == None:
return root
left = self.lowestCommonAncestor(root.left, p, q);
right = self.lowestCommonAncestor(root.right, p, q);
if left != None and right != None:
return root
if left == None:
return right;
return left