mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0102.二叉树的层序遍历.md
This commit is contained in:
@ -98,7 +98,7 @@ class Solution:
|
|||||||
out_list = []
|
out_list = []
|
||||||
|
|
||||||
while quene:
|
while quene:
|
||||||
length = len(queue) # 这里一定要先求出队列的长度,不能用range(len(queue)),因为queue长度是变化的
|
length = len(queue)
|
||||||
in_list = []
|
in_list = []
|
||||||
for _ in range(length):
|
for _ in range(length):
|
||||||
curnode = queue.pop(0) # (默认移除列表最后一个元素)这里需要移除队列最头上的那个
|
curnode = queue.pop(0) # (默认移除列表最后一个元素)这里需要移除队列最头上的那个
|
||||||
@ -627,6 +627,27 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
python代码:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def largestValues(self, root: TreeNode) -> List[int]:
|
||||||
|
if root is None:
|
||||||
|
return []
|
||||||
|
queue = [root]
|
||||||
|
out_list = []
|
||||||
|
while queue:
|
||||||
|
length = len(queue)
|
||||||
|
in_list = []
|
||||||
|
for _ in range(length):
|
||||||
|
curnode = queue.pop(0)
|
||||||
|
in_list.append(curnode.val)
|
||||||
|
if curnode.left: queue.append(curnode.left)
|
||||||
|
if curnode.right: queue.append(curnode.right)
|
||||||
|
out_list.append(max(in_list))
|
||||||
|
return out_list
|
||||||
|
```
|
||||||
|
|
||||||
javascript代码:
|
javascript代码:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
@ -712,6 +733,42 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
python代码:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 层序遍历解法
|
||||||
|
class Solution:
|
||||||
|
def connect(self, root: 'Node') -> 'Node':
|
||||||
|
if not root:
|
||||||
|
return None
|
||||||
|
queue = [root]
|
||||||
|
while queue:
|
||||||
|
n = len(queue)
|
||||||
|
for i in range(n):
|
||||||
|
node = queue.pop(0)
|
||||||
|
if node.left:
|
||||||
|
queue.append(node.left)
|
||||||
|
if node.right:
|
||||||
|
queue.append(node.right)
|
||||||
|
if i == n - 1:
|
||||||
|
break
|
||||||
|
node.next = queue[0]
|
||||||
|
return root
|
||||||
|
|
||||||
|
# 链表解法
|
||||||
|
class Solution:
|
||||||
|
def connect(self, root: 'Node') -> 'Node':
|
||||||
|
first = root
|
||||||
|
while first:
|
||||||
|
cur = first
|
||||||
|
while cur: # 遍历每一层的节点
|
||||||
|
if cur.left: cur.left.next = cur.right # 找左节点的next
|
||||||
|
if cur.right and cur.next: cur.right.next = cur.next.left # 找右节点的next
|
||||||
|
cur = cur.next # cur同层移动到下一节点
|
||||||
|
first = first.left # 从本层扩展到下一层
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
## 117.填充每个节点的下一个右侧节点指针II
|
## 117.填充每个节点的下一个右侧节点指针II
|
||||||
|
|
||||||
题目地址:https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/
|
题目地址:https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/
|
||||||
@ -753,7 +810,48 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
python代码:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 层序遍历解法
|
||||||
|
class Solution:
|
||||||
|
def connect(self, root: 'Node') -> 'Node':
|
||||||
|
if not root:
|
||||||
|
return None
|
||||||
|
queue = [root]
|
||||||
|
while queue: # 遍历每一层
|
||||||
|
length = len(queue)
|
||||||
|
tail = None # 每一层维护一个尾节点
|
||||||
|
for i in range(length): # 遍历当前层
|
||||||
|
curnode = queue.pop(0)
|
||||||
|
if tail:
|
||||||
|
tail.next = curnode # 让尾节点指向当前节点
|
||||||
|
tail = curnode # 让当前节点成为尾节点
|
||||||
|
if curnode.left : queue.append(curnode.left)
|
||||||
|
if curnode.right: queue.append(curnode.right)
|
||||||
|
return root
|
||||||
|
|
||||||
|
# 链表解法
|
||||||
|
class Solution:
|
||||||
|
def connect(self, root: 'Node') -> 'Node':
|
||||||
|
if not root:
|
||||||
|
return None
|
||||||
|
first = root
|
||||||
|
while first: # 遍历每一层
|
||||||
|
dummyHead = Node(None) # 为下一行创建一个虚拟头节点,相当于下一行所有节点链表的头结点(每一层都会创建);
|
||||||
|
tail = dummyHead # 为下一行维护一个尾节点指针(初始化是虚拟节点)
|
||||||
|
cur = first
|
||||||
|
while cur: # 遍历当前层的节点
|
||||||
|
if cur.left: # 链接下一行的节点
|
||||||
|
tail.next = cur.left
|
||||||
|
tail = tail.next
|
||||||
|
if cur.right:
|
||||||
|
tail.next = cur.right
|
||||||
|
tail = tail.next
|
||||||
|
cur = cur.next # cur同层移动到下一节点
|
||||||
|
first = dummyHead.next # 此处为换行操作,更新到下一行
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
## 总结
|
## 总结
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user