Skip to content

Commit e20c04b

Browse files
committed
1 parent 7728d5c commit e20c04b

File tree

14 files changed

+451
-197
lines changed

14 files changed

+451
-197
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
1. [二叉树的最近公共祖先](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README.md)
103103
1. [二叉搜索树的最近公共祖先](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README.md)
104104
1. [将二叉搜索树转换为单链表](/lcci/17.12.BiNode/README.md)
105+
1. [将二叉搜索树转化为排序的双向链表](/solution/0400-0499/0426.Convert%20Binary%20Search%20Tree%20to%20Sorted%20Doubly%20Linked%20List/README.md)
105106

106107
### 数学
107108

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
9898
1. [Lowest Common Ancestor of a Binary Tree](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_EN.md)
9999
1. [Lowest Common Ancestor of a Binary Search Tree](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README_EN.md)
100100
1. [BiNode](/lcci/17.12.BiNode/README_EN.md)
101+
1. [Convert Binary Search Tree to Sorted Doubly Linked List](/solution/0400-0499/0426.Convert%20Binary%20Search%20Tree%20to%20Sorted%20Doubly%20Linked%20List/README_EN.md)
101102

102103
### Math
103104

lcof/面试题36. 二叉搜索树与双向链表/README.md

+44-63
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
<!-- 这里可写通用的实现逻辑 -->
2828

29-
二叉搜索树中序遍历得到有序序列;根结点指向左子树最后一个结点,根结点也指向右子树第一个结点。
30-
31-
利用虚拟头结点递归遍历求解。
29+
- 排序链表:二叉搜索树中序遍历得到有序序列
30+
- 循环链表:头节点指向链表尾节点,尾节点指向链表头节点
31+
- 双向链表:`pre.right = cur``cur.left = pre``pre = cur`
3232

3333
<!-- tabs:start -->
3434

@@ -45,29 +45,26 @@ class Node:
4545
self.left = left
4646
self.right = right
4747
"""
48-
49-
5048
class Solution:
5149
def treeToDoublyList(self, root: 'Node') -> 'Node':
5250
def dfs(cur):
53-
if not cur:
51+
if cur is None:
5452
return
5553
dfs(cur.left)
56-
cur.left = self.lastNode
57-
if self.lastNode:
58-
self.lastNode.right = cur
59-
self.lastNode = cur
54+
if self.pre is None:
55+
self.head = cur
56+
else:
57+
self.pre.right = cur
58+
cur.left = self.pre
59+
self.pre = cur
6060
dfs(cur.right)
61-
62-
if not root:
63-
return root
64-
self.lastNode = head = Node(-1)
61+
if root is None:
62+
return None
63+
self.head = self.pre = None
6564
dfs(root)
66-
head = head.right
67-
head.left = self.lastNode
68-
self.lastNode.right = head
69-
return head
70-
65+
self.head.left = self.pre
66+
self.pre.right = self.head
67+
return self.head
7168
```
7269

7370
### **Java**
@@ -96,30 +93,23 @@ class Node {
9693
};
9794
*/
9895
class Solution {
99-
private Node lastNode;
96+
Node head;
97+
Node pre;
10098
public Node treeToDoublyList(Node root) {
101-
if (root == null) {
102-
return root;
103-
}
104-
lastNode = new Node(-1);
105-
Node head = lastNode;
99+
if (root == null) return null;
106100
dfs(root);
107-
head = head.right;
108-
head.left = lastNode;
109-
lastNode.right = head;
101+
head.left = pre;
102+
pre.right = head;
110103
return head;
111104
}
112105

113106
private void dfs(Node cur) {
114-
if (cur == null) {
115-
return;
116-
}
107+
if (cur == null) return;
117108
dfs(cur.left);
118-
cur.left = lastNode;
119-
if (lastNode != null) {
120-
lastNode.right = cur;
121-
}
122-
lastNode = cur;
109+
if (pre == null) head = cur;
110+
else pre.right = cur;
111+
cur.left = pre;
112+
pre = cur;
123113
dfs(cur.right);
124114
}
125115
}
@@ -130,41 +120,32 @@ class Solution {
130120
```js
131121
/**
132122
* // Definition for a Node.
133-
* function Node(val, left, right) {
134-
* this.val = val;
135-
* this.left = left;
136-
* this.right = right;
137-
* };
123+
* function Node(val,left,right) {
124+
* this.val = val;
125+
* this.left = left;
126+
* this.right = right;
127+
* };
138128
*/
139129
/**
140130
* @param {Node} root
141131
* @return {Node}
142132
*/
143133
var treeToDoublyList = function (root) {
144-
if (!root) return null;
145-
function dfs(node) {
146-
if (!node) return null;
147-
dfs(node.left);
148-
arr.push(node);
149-
dfs(node.right);
134+
function dfs(cur) {
135+
if (!cur) return;
136+
dfs(cur.left);
137+
if (!pre) head = cur;
138+
else pre.right = cur;
139+
cur.left = pre;
140+
pre = cur;
141+
dfs(cur.right);
150142
}
151-
let arr = [];
143+
if (!root) return null;
144+
let head, pre;
152145
dfs(root);
153-
let len = arr.length;
154-
let res = arr[0];
155-
for (let i = 0; i < len; i++) {
156-
if (i + 1 < len) {
157-
arr[i].right = arr[i + 1];
158-
} else {
159-
arr[i].right = arr[0];
160-
}
161-
if (i - 1 >= 0) {
162-
arr[i].left = arr[i - 1];
163-
} else {
164-
arr[i].left = arr[len - 1];
165-
}
166-
}
167-
return res;
146+
head.left = pre;
147+
pre.right = head;
148+
return head;
168149
};
169150
```
170151

lcof/面试题36. 二叉搜索树与双向链表/Solution.java

+10-17
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,23 @@ public Node(int _val,Node _left,Node _right) {
1919
};
2020
*/
2121
class Solution {
22-
private Node lastNode;
22+
Node head;
23+
Node pre;
2324
public Node treeToDoublyList(Node root) {
24-
if (root == null) {
25-
return root;
26-
}
27-
lastNode = new Node(-1);
28-
Node head = lastNode;
25+
if (root == null) return null;
2926
dfs(root);
30-
head = head.right;
31-
head.left = lastNode;
32-
lastNode.right = head;
27+
head.left = pre;
28+
pre.right = head;
3329
return head;
3430
}
3531

3632
private void dfs(Node cur) {
37-
if (cur == null) {
38-
return;
39-
}
33+
if (cur == null) return;
4034
dfs(cur.left);
41-
cur.left = lastNode;
42-
if (lastNode != null) {
43-
lastNode.right = cur;
44-
}
45-
lastNode = cur;
35+
if (pre == null) head = cur;
36+
else pre.right = cur;
37+
cur.left = pre;
38+
pre = cur;
4639
dfs(cur.right);
4740
}
4841
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
/**
22
* // Definition for a Node.
3-
* function Node(val, left, right) {
4-
* this.val = val;
5-
* this.left = left;
6-
* this.right = right;
7-
* };
3+
* function Node(val,left,right) {
4+
* this.val = val;
5+
* this.left = left;
6+
* this.right = right;
7+
* };
88
*/
99
/**
1010
* @param {Node} root
1111
* @return {Node}
1212
*/
1313
var treeToDoublyList = function (root) {
14-
if (!root) return null;
15-
function dfs(node) {
16-
if (!node) return null;
17-
dfs(node.left);
18-
arr.push(node);
19-
dfs(node.right);
14+
function dfs(cur) {
15+
if (!cur) return;
16+
dfs(cur.left);
17+
if (!pre) head = cur;
18+
else pre.right = cur;
19+
cur.left = pre;
20+
pre = cur;
21+
dfs(cur.right);
2022
}
21-
let arr = [];
23+
if (!root) return null;
24+
let head, pre;
2225
dfs(root);
23-
let len = arr.length;
24-
let res = arr[0];
25-
for (let i = 0; i < len; i++) {
26-
if (i + 1 < len) {
27-
arr[i].right = arr[i + 1];
28-
} else {
29-
arr[i].right = arr[0];
30-
}
31-
if (i - 1 >= 0) {
32-
arr[i].left = arr[i - 1];
33-
} else {
34-
arr[i].left = arr[len - 1];
35-
}
36-
}
37-
return res;
26+
head.left = pre;
27+
pre.right = head;
28+
return head;
3829
};

lcof/面试题36. 二叉搜索树与双向链表/Solution.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@ def __init__(self, val, left=None, right=None):
66
self.left = left
77
self.right = right
88
"""
9-
10-
119
class Solution:
1210
def treeToDoublyList(self, root: 'Node') -> 'Node':
1311
def dfs(cur):
14-
if not cur:
12+
if cur is None:
1513
return
1614
dfs(cur.left)
17-
cur.left = self.lastNode
18-
if self.lastNode:
19-
self.lastNode.right = cur
20-
self.lastNode = cur
15+
if self.pre is None:
16+
self.head = cur
17+
else:
18+
self.pre.right = cur
19+
cur.left = self.pre
20+
self.pre = cur
2121
dfs(cur.right)
22-
23-
if not root:
24-
return root
25-
self.lastNode = head = Node(-1)
22+
if root is None:
23+
return None
24+
self.head = self.pre = None
2625
dfs(root)
27-
head = head.right
28-
head.left = self.lastNode
29-
self.lastNode.right = head
30-
return head
26+
self.head.left = self.pre
27+
self.pre.right = self.head
28+
return self.head

solution/0000-0099/0020.Valid Parentheses/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ class Solution {
9797
}
9898
```
9999

100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
bool isValid(string s) {
106+
stack<char> q;
107+
for (int i = 0, n = s.length(); i < n; ++i) {
108+
if (s[i] == '{' || s[i] == '[' || s[i] == '(') q.push(s[i]);
109+
else if (q.empty() || !match(q.top(), s[i])) return false;
110+
else q.pop();
111+
}
112+
return q.empty();
113+
}
114+
private:
115+
bool match(char l, char r) {
116+
return (l == '(' && r == ')') || (l == '[' && r == ']') || (l == '{' && r == '}');
117+
}
118+
};
119+
```
120+
100121
### **...**
101122
102123
```

solution/0000-0099/0020.Valid Parentheses/README_EN.md

+21
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ class Solution {
105105
}
106106
```
107107

108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
bool isValid(string s) {
114+
stack<char> q;
115+
for (int i = 0, n = s.length(); i < n; ++i) {
116+
if (s[i] == '{' || s[i] == '[' || s[i] == '(') q.push(s[i]);
117+
else if (q.empty() || !match(q.top(), s[i])) return false;
118+
else q.pop();
119+
}
120+
return q.empty();
121+
}
122+
private:
123+
bool match(char l, char r) {
124+
return (l == '(' && r == ')') || (l == '[' && r == ']') || (l == '{' && r == '}');
125+
}
126+
};
127+
```
128+
108129
### **...**
109130
110131
```

0 commit comments

Comments
 (0)