auto commit
This commit is contained in:
@ -477,7 +477,7 @@ System.out.println(InterfaceExample.x);
|
|||||||
- 从设计层面上看,抽象类提供了一种 IS-A 关系,那么就必须满足里式替换原则,即子类对象必须能够替换掉所有父类对象。而接口更像是一种 LIKE-A 关系,它只是提供一种方法实现契约,并不要求接口和实现接口的类具有 IS-A 关系。
|
- 从设计层面上看,抽象类提供了一种 IS-A 关系,那么就必须满足里式替换原则,即子类对象必须能够替换掉所有父类对象。而接口更像是一种 LIKE-A 关系,它只是提供一种方法实现契约,并不要求接口和实现接口的类具有 IS-A 关系。
|
||||||
- 从使用上来看,一个类可以实现多个接口,但是不能继承多个抽象类。
|
- 从使用上来看,一个类可以实现多个接口,但是不能继承多个抽象类。
|
||||||
- 接口的字段只能是 static 和 final 类型的,而抽象类的字段没有这种限制。
|
- 接口的字段只能是 static 和 final 类型的,而抽象类的字段没有这种限制。
|
||||||
- 接口的方法只能是 public 的,而抽象类的方法可以由多种访问权限。
|
- 接口的方法只能是 public 的,而抽象类的方法可以有多种访问权限。
|
||||||
|
|
||||||
**4. 使用选择**
|
**4. 使用选择**
|
||||||
|
|
||||||
|
@ -5728,10 +5728,10 @@ public List<Double> averageOfLevels(TreeNode root) {
|
|||||||
if (root == null) return ret;
|
if (root == null) return ret;
|
||||||
Queue<TreeNode> queue = new LinkedList<>();
|
Queue<TreeNode> queue = new LinkedList<>();
|
||||||
queue.add(root);
|
queue.add(root);
|
||||||
while (!queue.isEmpty()){
|
while (!queue.isEmpty()) {
|
||||||
int cnt = queue.size();
|
int cnt = queue.size();
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
for (int i = 0; i < cnt; i++){
|
for (int i = 0; i < cnt; i++) {
|
||||||
TreeNode node = queue.poll();
|
TreeNode node = queue.poll();
|
||||||
sum += node.val;
|
sum += node.val;
|
||||||
if (node.left != null) queue.add(node.left);
|
if (node.left != null) queue.add(node.left);
|
||||||
@ -5766,7 +5766,7 @@ Output:
|
|||||||
public int findBottomLeftValue(TreeNode root) {
|
public int findBottomLeftValue(TreeNode root) {
|
||||||
Queue<TreeNode> queue = new LinkedList<>();
|
Queue<TreeNode> queue = new LinkedList<>();
|
||||||
queue.add(root);
|
queue.add(root);
|
||||||
while (!queue.isEmpty()){
|
while (!queue.isEmpty()) {
|
||||||
root = queue.poll();
|
root = queue.poll();
|
||||||
if (root.right != null) queue.add(root.right);
|
if (root.right != null) queue.add(root.right);
|
||||||
if (root.left != null) queue.add(root.left);
|
if (root.left != null) queue.add(root.left);
|
||||||
@ -5785,10 +5785,10 @@ public int findBottomLeftValue(TreeNode root) {
|
|||||||
4 5 6
|
4 5 6
|
||||||
```
|
```
|
||||||
|
|
||||||
层次遍历顺序:[1 2 3 4 5 6]
|
- 层次遍历顺序:[1 2 3 4 5 6]
|
||||||
前序遍历顺序:[1 2 4 5 3 6]
|
- 前序遍历顺序:[1 2 4 5 3 6]
|
||||||
中序遍历顺序:[4 2 5 1 3 6]
|
- 中序遍历顺序:[4 2 5 1 3 6]
|
||||||
后序遍历顺序:[4 5 2 6 3 1]
|
- 后序遍历顺序:[4 5 2 6 3 1]
|
||||||
|
|
||||||
层次遍历使用 BFS 实现,利用的就是 BFS 一层一层遍历的特性;而前序、中序、后序遍历利用了 DFS 实现。
|
层次遍历使用 BFS 实现,利用的就是 BFS 一层一层遍历的特性;而前序、中序、后序遍历利用了 DFS 实现。
|
||||||
|
|
||||||
@ -5797,7 +5797,7 @@ public int findBottomLeftValue(TreeNode root) {
|
|||||||
① 前序
|
① 前序
|
||||||
|
|
||||||
```java
|
```java
|
||||||
void dfs(TreeNode root){
|
void dfs(TreeNode root) {
|
||||||
visit(root);
|
visit(root);
|
||||||
dfs(root.left);
|
dfs(root.left);
|
||||||
dfs(root.right);
|
dfs(root.right);
|
||||||
@ -5807,7 +5807,7 @@ void dfs(TreeNode root){
|
|||||||
② 中序
|
② 中序
|
||||||
|
|
||||||
```java
|
```java
|
||||||
void dfs(TreeNode root){
|
void dfs(TreeNode root) {
|
||||||
dfs(root.left);
|
dfs(root.left);
|
||||||
visit(root);
|
visit(root);
|
||||||
dfs(root.right);
|
dfs(root.right);
|
||||||
@ -5817,7 +5817,7 @@ void dfs(TreeNode root){
|
|||||||
③ 后序
|
③ 后序
|
||||||
|
|
||||||
```java
|
```java
|
||||||
void dfs(TreeNode root){
|
void dfs(TreeNode root) {
|
||||||
dfs(root.left);
|
dfs(root.left);
|
||||||
dfs(root.right);
|
dfs(root.right);
|
||||||
visit(root);
|
visit(root);
|
||||||
@ -5837,7 +5837,7 @@ public List<Integer> preorderTraversal(TreeNode root) {
|
|||||||
TreeNode node = stack.pop();
|
TreeNode node = stack.pop();
|
||||||
if (node == null) continue;
|
if (node == null) continue;
|
||||||
ret.add(node.val);
|
ret.add(node.val);
|
||||||
stack.push(node.right); // 先右后左,保证左子树先遍历
|
stack.push(node.right); // 先右后左,保证左子树先遍历
|
||||||
stack.push(node.left);
|
stack.push(node.left);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
@ -5923,7 +5923,7 @@ Output:
|
|||||||
1
|
1
|
||||||
```
|
```
|
||||||
|
|
||||||
只保留值在 L \~ R 之间的节点
|
题目描述:只保留值在 L \~ R 之间的节点
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public TreeNode trimBST(TreeNode root, int L, int R) {
|
public TreeNode trimBST(TreeNode root, int L, int R) {
|
||||||
@ -6008,12 +6008,12 @@ public TreeNode convertBST(TreeNode root) {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void traver(TreeNode root) {
|
private void traver(TreeNode node) {
|
||||||
if (root == null) return;
|
if (node == null) return;
|
||||||
if (root.right != null) traver(root.right);
|
traver(node.right);
|
||||||
sum += root.val;
|
sum += node.val;
|
||||||
root.val = sum;
|
node.val = sum;
|
||||||
if (root.left != null) traver(root.left);
|
traver(node.left);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -6085,7 +6085,6 @@ private TreeNode toBST(int[] nums, int sIdx, int eIdx){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
**根据有序链表构造平衡的二叉查找树**
|
**根据有序链表构造平衡的二叉查找树**
|
||||||
|
|
||||||
[109. Convert Sorted List to Binary Search Tree (Medium)](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/)
|
[109. Convert Sorted List to Binary Search Tree (Medium)](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/)
|
||||||
@ -6105,29 +6104,25 @@ One possible answer is: [0,-3,9,-10,null,5], which represents the following heig
|
|||||||
```java
|
```java
|
||||||
public TreeNode sortedListToBST(ListNode head) {
|
public TreeNode sortedListToBST(ListNode head) {
|
||||||
if (head == null) return null;
|
if (head == null) return null;
|
||||||
int size = size(head);
|
if (head.next == null) return new TreeNode(head.val);
|
||||||
if (size == 1) return new TreeNode(head.val);
|
ListNode preMid = preMid(head);
|
||||||
ListNode pre = head, mid = pre.next;
|
ListNode mid = preMid.next;
|
||||||
int step = 2;
|
preMid.next = null; // 断开链表
|
||||||
while (step <= size / 2) {
|
|
||||||
pre = mid;
|
|
||||||
mid = mid.next;
|
|
||||||
step++;
|
|
||||||
}
|
|
||||||
pre.next = null;
|
|
||||||
TreeNode t = new TreeNode(mid.val);
|
TreeNode t = new TreeNode(mid.val);
|
||||||
t.left = sortedListToBST(head);
|
t.left = sortedListToBST(head);
|
||||||
t.right = sortedListToBST(mid.next);
|
t.right = sortedListToBST(mid.next);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int size(ListNode node) {
|
private ListNode preMid(ListNode head) {
|
||||||
int size = 0;
|
ListNode slow = head, fast = head.next;
|
||||||
while (node != null) {
|
ListNode pre = head;
|
||||||
size++;
|
while (fast != null && fast.next != null) {
|
||||||
node = node.next;
|
pre = slow;
|
||||||
|
slow = slow.next;
|
||||||
|
fast = fast.next.next;
|
||||||
}
|
}
|
||||||
return size;
|
return pre;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -6207,7 +6202,7 @@ public int getMinimumDifference(TreeNode root) {
|
|||||||
private void inOrder(TreeNode node) {
|
private void inOrder(TreeNode node) {
|
||||||
if (node == null) return;
|
if (node == null) return;
|
||||||
inOrder(node.left);
|
inOrder(node.left);
|
||||||
if (preNode != null) minDiff = Math.min(minDiff, Math.abs(node.val - preNode.val));
|
if (preNode != null) minDiff = Math.min(minDiff, node.val - preNode.val);
|
||||||
preNode = node;
|
preNode = node;
|
||||||
inOrder(node.right);
|
inOrder(node.right);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user