auto commit
This commit is contained in:
@ -4293,6 +4293,8 @@ class Tuple implements Comparable<Tuple> {
|
|||||||
|
|
||||||
## 链表
|
## 链表
|
||||||
|
|
||||||
|
链表是空节点,或者有一个值和一个指向下一个链表的指针,因此很多链表问题可以用递归来处理。
|
||||||
|
|
||||||
**找出两个链表的交点**
|
**找出两个链表的交点**
|
||||||
|
|
||||||
[Leetcode : 160. Intersection of Two Linked Lists (Easy)](https://leetcode.com/problems/intersection-of-two-linked-lists/description/)
|
[Leetcode : 160. Intersection of Two Linked Lists (Easy)](https://leetcode.com/problems/intersection-of-two-linked-lists/description/)
|
||||||
@ -4305,7 +4307,7 @@ A: a1 → a2
|
|||||||
B: b1 → b2 → b3
|
B: b1 → b2 → b3
|
||||||
```
|
```
|
||||||
|
|
||||||
要求:时间复杂度为 O(n) 空间复杂度为 O(1)
|
要求:时间复杂度为 O(N) 空间复杂度为 O(1)
|
||||||
|
|
||||||
设 A 的长度为 a + c,B 的长度为 b + c,其中 c 为尾部公共部分长度,可知 a + c + b = b + c + a。
|
设 A 的长度为 a + c,B 的长度为 b + c,其中 c 为尾部公共部分长度,可知 a + c + b = b + c + a。
|
||||||
|
|
||||||
@ -4313,7 +4315,6 @@ B: b1 → b2 → b3
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||||
if(headA == null || headB == null) return null;
|
|
||||||
ListNode l1 = headA, l2 = headB;
|
ListNode l1 = headA, l2 = headB;
|
||||||
while(l1 != l2){
|
while(l1 != l2){
|
||||||
l1 = (l1 == null) ? headB : l1.next;
|
l1 = (l1 == null) ? headB : l1.next;
|
||||||
@ -4329,40 +4330,49 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
|||||||
|
|
||||||
[Leetcode : 206. Reverse Linked List (Easy)](https://leetcode.com/problems/reverse-linked-list/description/)
|
[Leetcode : 206. Reverse Linked List (Easy)](https://leetcode.com/problems/reverse-linked-list/description/)
|
||||||
|
|
||||||
头插法能够按逆序构建链表。
|
递归
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public ListNode reverseList(ListNode head) {
|
public ListNode reverseList(ListNode head) {
|
||||||
ListNode newHead = null; // 设为 null,作为新链表的结尾
|
if (head == null || head.next == null) return head;
|
||||||
while(head != null){
|
ListNode next = head.next;
|
||||||
ListNode nextNode = head.next;
|
ListNode newHead = reverseList(next);
|
||||||
head.next = newHead;
|
next.next = head;
|
||||||
newHead = head;
|
head.next = null;
|
||||||
head = nextNode;
|
|
||||||
}
|
|
||||||
return newHead;
|
return newHead;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
头插法
|
||||||
|
|
||||||
|
```java
|
||||||
|
public ListNode reverseList(ListNode head) {
|
||||||
|
ListNode newHead = new ListNode(-1);
|
||||||
|
while (head != null) {
|
||||||
|
ListNode next = head.next;
|
||||||
|
head.next = newHead.next;
|
||||||
|
newHead.next = head;
|
||||||
|
head = next;
|
||||||
|
}
|
||||||
|
return newHead.next;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
**归并两个有序的链表**
|
**归并两个有序的链表**
|
||||||
|
|
||||||
[Leetcode : 21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists/description/)
|
[Leetcode : 21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists/description/)
|
||||||
|
|
||||||
链表和树一样,可以用递归方式来定义:链表是空节点,或者有一个值和一个指向下一个链表的指针。因此很多链表问题可以用递归来处理。
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
|
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
|
||||||
if (l1 == null) return l2;
|
if (l1 == null) return l2;
|
||||||
if (l2 == null) return l1;
|
if (l2 == null) return l1;
|
||||||
ListNode newHead = null;
|
|
||||||
if (l1.val < l2.val) {
|
if (l1.val < l2.val) {
|
||||||
newHead = l1;
|
l1.next = mergeTwoLists(l1.next, l2);
|
||||||
newHead.next = mergeTwoLists(l1.next, l2);
|
return l1;
|
||||||
} else {
|
} else {
|
||||||
newHead = l2;
|
l2.next = mergeTwoLists(l1, l2.next);
|
||||||
newHead.next = mergeTwoLists(l1, l2.next);
|
return l2;
|
||||||
}
|
}
|
||||||
return newHead;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -4772,6 +4782,53 @@ private int pathSumStartWithRoot(TreeNode root, int sum){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**子树**
|
||||||
|
|
||||||
|
[Leetcode : 572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/)
|
||||||
|
|
||||||
|
```html
|
||||||
|
Given tree s:
|
||||||
|
3
|
||||||
|
/ \
|
||||||
|
4 5
|
||||||
|
/ \
|
||||||
|
1 2
|
||||||
|
Given tree t:
|
||||||
|
4
|
||||||
|
/ \
|
||||||
|
1 2
|
||||||
|
Return true, because t has the same structure and node values with a subtree of s.
|
||||||
|
|
||||||
|
Given tree s:
|
||||||
|
|
||||||
|
3
|
||||||
|
/ \
|
||||||
|
4 5
|
||||||
|
/ \
|
||||||
|
1 2
|
||||||
|
/
|
||||||
|
0
|
||||||
|
Given tree t:
|
||||||
|
4
|
||||||
|
/ \
|
||||||
|
1 2
|
||||||
|
Return false.
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
public boolean isSubtree(TreeNode s, TreeNode t) {
|
||||||
|
if (s == null) return false;
|
||||||
|
return isSubtreeWithRoot(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSubtreeWithRoot(TreeNode s, TreeNode t) {
|
||||||
|
if (t == null && s == null) return true;
|
||||||
|
if (t == null || s == null) return false;
|
||||||
|
if (t.val != s.val) return false;
|
||||||
|
return isSubtreeWithRoot(s.left, t.left) && isSubtreeWithRoot(s.right, t.right);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
**树的对称**
|
**树的对称**
|
||||||
|
|
||||||
[Leetcode : 101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/)
|
[Leetcode : 101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/)
|
||||||
@ -4912,40 +4969,6 @@ public TreeNode trimBST(TreeNode root, int L, int R) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**子树**
|
|
||||||
|
|
||||||
[Leetcode : 572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/)
|
|
||||||
|
|
||||||
```html
|
|
||||||
Given tree s:
|
|
||||||
3
|
|
||||||
/ \
|
|
||||||
4 5
|
|
||||||
/ \
|
|
||||||
1 2
|
|
||||||
Given tree t:
|
|
||||||
4
|
|
||||||
/ \
|
|
||||||
1 2
|
|
||||||
Return true, because t has the same structure and node values with a subtree of s.
|
|
||||||
```
|
|
||||||
|
|
||||||
```java
|
|
||||||
public boolean isSubtree(TreeNode s, TreeNode t) {
|
|
||||||
if(s == null && t == null) return true;
|
|
||||||
if(s == null || t == null) return false;
|
|
||||||
if(s.val == t.val && isSame(s, t)) return true;
|
|
||||||
return isSubtree(s.left, t) || isSubtree(s.right, t);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isSame(TreeNode s, TreeNode t){
|
|
||||||
if(s == null && t == null) return true;
|
|
||||||
if(s == null || t == null) return false;
|
|
||||||
if(s.val != t.val) return false;
|
|
||||||
return isSame(s.left, t.left) && isSame(s.right, t.right);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**从有序数组中构造二叉查找树**
|
**从有序数组中构造二叉查找树**
|
||||||
|
|
||||||
[Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
|
[Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
|
||||||
@ -5091,6 +5114,7 @@ Output : 2
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
private int path = 0;
|
private int path = 0;
|
||||||
|
|
||||||
public int longestUnivaluePath(TreeNode root) {
|
public int longestUnivaluePath(TreeNode root) {
|
||||||
dfs(root);
|
dfs(root);
|
||||||
return path;
|
return path;
|
||||||
|
Reference in New Issue
Block a user