auto commit

This commit is contained in:
CyC2018
2018-06-25 13:46:38 +08:00
parent af5c7e9390
commit d82f2d5697
2 changed files with 32 additions and 37 deletions

View File

@ -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. 使用选择**

View File

@ -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 实现。
@ -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);
} }