Skip to content

Commit e99ae6b

Browse files
authored
add 94
1 parent 258b490 commit e99ae6b

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,37 @@ class Solution:
541541
def grayCode(self, n: int) -> List[int]:
542542
return [i ^ i >> 1 for i in range(1 << n)]
543543
```
544+
## [94. Binary Tree Inorder Traversal 2行](https://leetcode.com/problems/binary-tree-inorder-traversal/)
545+
```python
546+
# Definition for a binary tree node.
547+
# class TreeNode:
548+
# def __init__(self, x):
549+
# self.val = x
550+
# self.left = None
551+
# self.right = None
552+
553+
class Solution:
554+
def inorderTraversal(self, root: TreeNode) -> List[int]:
555+
f = self.inorderTraversal
556+
return f(root.left) + [root.val] + f(root.right) if root else []
557+
```
558+
- 递归
559+
```python
560+
class Solution:
561+
def inorderTraversal(self, root: TreeNode) -> List[int]:
562+
r, stack = [], []
563+
while True:
564+
while root:
565+
stack.append(root)
566+
root = root.left
567+
if not stack:
568+
return r
569+
node = stack.pop()
570+
r.append(node.val)
571+
root = node.right
572+
return r
573+
```
574+
- 迭代
544575
## [104. Maximum Depth of Binary Tree 1行](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
545576
```python
546577
# Definition for a binary tree node.
@@ -1210,7 +1241,7 @@ class Solution:
12101241
- 缺失数字 = 0 加到 n+1 的总和 - 数组中所有数字的总和
12111242
- 计算 0 加到 n+1 的总和,可利用等差数列求和公式,此题可理解为`总和 = (元素个数 / 2) * (首尾两数字之和)`
12121243
## 双指针
1213-
### [344. Reverse String](https://leetcode.com/problems/reverse-string/)
1244+
### [344. Reverse String 双指针](https://leetcode.com/problems/reverse-string/)
12141245
```python
12151246
class Solution:
12161247
def reverseString(self, s: List[str]) -> None:

0 commit comments

Comments
 (0)