学习目标:
60天训练营打卡计划!
学习内容:
235. 二叉搜索树的最近公共祖先
- 充分的利用二叉搜索树的性质:有确定的搜索方向;且当根节点落在p和q之间时,该根节点一定是最近公共祖先。
- 236的普通思路依旧适用于该题。
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null) return root;
if(root.val > p.val && root.val > q.val){
TreeNode left = lowestCommonAncestor(root.left, p, q);
if(left != null)
return left;
}
else if(root.val < p.val && root.val < q.val){
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(right != null)
return right;
}
return root;
}
}
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null) return root;
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;
if(left == null && right != null) return right;
return null;
}
}
701.二叉搜索树中的插入操作
- 在叶子节点处加入新节点,在递归结束处对节点进行增加操作。
- 题目中说了加入的新节点是原树中没有的,否则就不加入。
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root == null){
TreeNode node = new TreeNode(val);
return node;
}
if(val < root.val)
root.left = insertIntoBST(root.left, val);
if(val > root.val)
root.right = insertIntoBST(root.right, val);
return root;
}
}
450.删除二叉搜索树中的节点
- 在结束条件时,分情况对节点进行删除。可以分为以下的5种情况:
①未找到要删除的节点;
②要删除的节点是叶子节点(无左无右)
③要删除的节点有左节点
④要删除的节点有右节点
⑤要删除的节点既有左节点也有右节点。
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return null;
if(root.val == key){
if(root.left == null && root.right == null) return null;
if(root.left != null && root.right == null) return root.left;
if(root.left == null && root.right != null) return root.right;
if(root.left != null && root.right != null){
TreeNode node = root.right;
while(node.left != null) node = node.left;
node.left = root.left;
return root.right;
}
}
if(root.val > key) root.left = deleteNode(root.left, key);
if(root.val < key) root.right = deleteNode(root.right, key);
return root;
}
}
1365. 有多少小于当前数字的数字
- 先对传入的数组排序后即可得到小于当前数字的数字。
- 熟悉对数组的复制和排序api
Arrays.sort(sortednums);
Arrays.copyOf(nums, nums.length);
class Solution {
public int[] smallerNumbersThanCurrent(int[] nums) {
int[] sortednums = Arrays.copyOf(nums, nums.length);
Arrays.sort(sortednums);
int[] res = new int[nums.length];
int j = 0;
for(int i = 0; i < nums.length; i++){
while(sortednums[j] != nums[i]){
j++;
}
res[i] = j;
j = 0;
}
return res;
}
}
129. 求根节点到叶节点数字之和
- 没想清楚的就是为什么结束条件变成了node.left == null && node.right == null?
① node == null 作为终止条件:
当问题需要遍历整棵树的所有节点时,通常使用 node == null 作为终止条件。这种情况适用于遍历整个树的场景,比如树的搜索、求深度、路径遍历等。
② node.left == null && node.right == null 作为终止条件:
当问题需要针对叶子节点进行特定操作时,例如寻找叶子节点、计算叶子节点的高度或路径、使用叶子结点的值等,此时使用 node.left == null && node.right == null 作为终止条件更为合适。
class Solution {
int allSum = 0;
int sum = 0;
List<Integer> list = new ArrayList<>();
private int Array2Int(List<Integer> lis){
int res = 0;
for(int i = 0; i < lis.size(); i++){
res = res * 10 + lis.get(i);
}
return res;
}
private void traversal(TreeNode node){
if(node.left == null && node.right == null){
allSum += Array2Int(list);
return;
}
if(node.left != null){
list.add(node.left.val);
traversal(node.left);
list.remove(list.size() - 1);
}
if(node.right != null){
list.add(node.right.val);
traversal(node.right);
list.remove(list.size() - 1);
}
return ;
}
public int sumNumbers(TreeNode root) {
list.add(root.val);
traversal(root);
return allSum;
}
}
学习时间: