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/)
|
||||
@ -4305,7 +4307,7 @@ A: a1 → a2
|
||||
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。
|
||||
|
||||
@ -4313,7 +4315,6 @@ B: b1 → b2 → b3
|
||||
|
||||
```java
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
if(headA == null || headB == null) return null;
|
||||
ListNode l1 = headA, l2 = headB;
|
||||
while(l1 != l2){
|
||||
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/)
|
||||
|
||||
头插法能够按逆序构建链表。
|
||||
递归
|
||||
|
||||
```java
|
||||
public ListNode reverseList(ListNode head) {
|
||||
ListNode newHead = null; // 设为 null,作为新链表的结尾
|
||||
while(head != null){
|
||||
ListNode nextNode = head.next;
|
||||
head.next = newHead;
|
||||
newHead = head;
|
||||
head = nextNode;
|
||||
}
|
||||
if (head == null || head.next == null) return head;
|
||||
ListNode next = head.next;
|
||||
ListNode newHead = reverseList(next);
|
||||
next.next = head;
|
||||
head.next = null;
|
||||
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/)
|
||||
|
||||
链表和树一样,可以用递归方式来定义:链表是空节点,或者有一个值和一个指向下一个链表的指针。因此很多链表问题可以用递归来处理。
|
||||
|
||||
```java
|
||||
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
|
||||
if (l1 == null) return l2;
|
||||
if (l2 == null) return l1;
|
||||
ListNode newHead = null;
|
||||
if (l1.val < l2.val) {
|
||||
newHead = l1;
|
||||
newHead.next = mergeTwoLists(l1.next, l2);
|
||||
l1.next = mergeTwoLists(l1.next, l2);
|
||||
return l1;
|
||||
} else {
|
||||
newHead = l2;
|
||||
newHead.next = mergeTwoLists(l1, l2.next);
|
||||
l2.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/)
|
||||
@ -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/)
|
||||
@ -5091,6 +5114,7 @@ Output : 2
|
||||
|
||||
```java
|
||||
private int path = 0;
|
||||
|
||||
public int longestUnivaluePath(TreeNode root) {
|
||||
dfs(root);
|
||||
return path;
|
||||
|
Reference in New Issue
Block a user