/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
TreeNode pre = null;
int result = Integer.MAX_VALUE;//为了承接差值的最小值
public int getMinimumDifference(TreeNode root) {
if(root == null) return 0;
traversal(root);
return result;
}
public void traversal(TreeNode root){
//左
if(root == null) return;
traversal(root.left);
//中
if(pre != null){
result = Math.min(result, root.val - pre.val);
}
pre = root;
//右
traversal(root.right);
}
}
思路:
- 中序遍历:由于BST的性质(左子树节点的值 < 根节点的值 < 右子树节点的值),中序遍历将按升序顺序输出所有节点的值。
- 维护前一个节点:在中序遍历时,我们维护一个前一个遍历节点
pre
。对于当前遍历的节点,与pre
节点的差值可能是最小绝对差值的候选。 - 更新结果:对于每一个非首节点,计算与前一个节点的差,并尝试用它更新
result
。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
ArrayList<Integer> resList;
int maxCount;
int count;
TreeNode pre;
public int[] findMode(TreeNode root) {
resList = new ArrayList<>();
maxCount = 0;
count = 0;
pre = null;
findModel1(root);
int [] res = new int[resList.size()];
for(int i = 0; i < resList.size(); i++){
res[i] = resList.get(i);
}
return res;
}
public void findModel1(TreeNode root){
if(root == null){
return;
}
//左
findModel1(root.left);
//中
int rootValue = root.val;
if(pre == null || rootValue != pre.val){
count = 1;
} else{
count++;
}
if(count > maxCount){
resList.clear();
resList.add(rootValue);
maxCount = count;
}else if(count == maxCount){
resList.add(rootValue);
}
pre = root;
//中
findModel1(root.right);
}
}
思路:
-
初始化:
resList
:这是一个动态数组,用于保存所有出现次数最多的元素。maxCount
:当前已知的最大出现次数。count
:用于计算当前元素的出现次数。pre
:上一个遍历的节点。这个指针用于比较当前节点和上一个节点的值,以确定我们是否应该重置count
。
-
中序遍历:
findMode1
函数是一个递归函数,用于中序遍历树。- 先遍历左子树,然后处理根节点,最后遍历右子树。
- 对于每一个遍历到的节点,都会比较其值和
pre
节点的值:- 如果
pre
为null
(即这是遍历的第一个节点)或当前节点的值不等于pre
的值,那么count
被设置为1。 - 否则,
count
增加1。
- 如果
- 接下来,我们将
count
与maxCount
进行比较:- 如果
count
大于maxCount
,我们清空resList
,并将当前节点的值添加到其中,同时更新maxCount
的值。 - 如果
count
等于maxCount
,则直接将当前节点的值添加到resList
中。
- 如果
- 最后,将
pre
设置为当前节点,并遍历右子树。
-
结果转换:
- 将
resList
转换为一个数组,并返回。
- 将
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || 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 null;
}else if(left == null && right != null){
return right;
}else if(left != null && right == null){
return left;
}else{
return root;
}
}
}
思路:
-
if(root == null || root == p || root == q)
:这个条件判断当前节点是否是null
或者是否是我们要找的其中一个节点。如果是,则返回当前节点。 -
接下来,我们递归地在左子树和右子树中寻找
p
和q
:-
TreeNode left = lowestCommonAncestor(root.left, p, q);
:在左子树中查找。 -
TreeNode right = lowestCommonAncestor(root.right, p, q);
:在右子树中查找。
-
-
根据左右子树的返回值来确定最低公共祖先:
-
如果
left
和right
都为null
,说明p
和q
都不在当前节点的子树中,因此返回null
。 -
如果
left
为null
而right
不为null
,说明只有right
子树中有p
或q
,因此返回right
。 -
如果
left
不为null
而right
为null
,说明只有left
子树中有p
或q
,因此返回left
。 -
如果
left
和right
都不为null
,说明p
和q
分别在当前节点的左右子树中,因此当前节点就是它们的最低公共祖先,返回root
。
-