#LeetCode 530. Minimum Absolute Difference in BST
#LeetCode 530. 视频讲解:二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差_哔哩哔哩_bilibili
定义一个新的函数时,考虑返回值类型。如果需要返回值,那么说明这个函数需要返回某个特性、特点、数值。如果不需要遍历整个二叉树,只需要某个特定路径或者某个节点。在本题中,定义了一个全局变量记录了minimal value,所以不需要函数具有返回值。
递归法(使用双指针)代码:
/**
* 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) {
traversal(root);
return result;
}
public void traversal(TreeNode cur) {
if (cur == null) {
return;
}
traversal(cur.left);
if(pre != null) {
result = Math.min(result, cur.val - pre.val);
}
pre = cur;
traversal(cur.right);
}
}
#LeetCode 501. Find Mode in Binary Search Tree
#LeetCode 501. 视频讲解:不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数_哔哩哔哩_bilibili
建立全局变量:pre 指针(指向当前节点的前一个节点)、maxCount 变量(记录目前遍历到的最大数目)、count (记录当前节点的重复数目)、ArrayList 类型的result(由于最大重复的数目不知道,所以设置可变长度的ArrayList 类型,在return 之前将ArrayList 转换为int 数组)。如果看到是二叉搜索树,那么考虑用中序遍历。
迭代法代码:
/**
* 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 {
int count = 0;
int maxCount = 0;
ArrayList<Integer> result;
TreeNode pre = null;
public int[] findMode(TreeNode root) {
result = new ArrayList<>();
traversal(root);
int[] res = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
res[i] = result.get(i);
}
return res;
}
public void traversal(TreeNode cur) {
if (cur == null) {
return;
}
traversal(cur.left);
if (pre == null) {
count = 1;
}
else if (pre.val == cur.val) {
count += 1;
}
else {
count = 1;
}
pre = cur;
if (maxCount == count) {
result.add(cur.val);
}
if (maxCount < count) {
maxCount = count;
result.clear();
result.add(cur.val);
}
traversal(cur.right);
}
}
#LeetCode 236. Lowest Common Ancestor of a Binary Tree
#LeetCode 236. 视频讲解:自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先_哔哩哔哩_bilibili
使用后序遍历,左右中的“中”体现了回溯,是通过左右子树的返回值,处理中节点。递归的终止条件是遇到了节点p 或者节点q ,则向上返回。本题的巧妙在于如果设置的left 节点变量为空,right 节点变量不为空,就返回right ,说明目标节点是通过right 返回的。如果设置的right 节点变量为空,left 节点变量不为空,就返回left ,说明目标节点是通过left 返回的。如果两个节点都为空,则返回left 或者right 都可以,或者直接返回null。
迭代 + 回溯代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (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;
}
if (left != null && right == null) {
return left;
} else if (left == null && right != null) {
return right;
} else return null;
}
}