Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0135feb

Browse files
Merge pull requestyoungyangyang04#419 from Miraclelucy/master
Update 0102.二叉树的层序遍历.md - 增加515、116、117题的python3解法
2 parentsb33e7b3 +45b88a6 commit0135feb

File tree

1 file changed

+99
-1
lines changed

1 file changed

+99
-1
lines changed

‎problems/0102.二叉树的层序遍历.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Solution:
9898
out_list= []
9999

100100
while quene:
101-
length=len(queue)# 这里一定要先求出队列的长度,不能用range(len(queue)),因为queue长度是变化的
101+
length=len(queue)
102102
in_list= []
103103
for _inrange(length):
104104
curnode= queue.pop(0)# (默认移除列表最后一个元素)这里需要移除队列最头上的那个
@@ -627,6 +627,27 @@ public:
627627
}
628628
};
629629
```
630+
python代码:
631+
632+
```python
633+
class Solution:
634+
def largestValues(self, root: TreeNode) -> List[int]:
635+
if root is None:
636+
return []
637+
queue = [root]
638+
out_list = []
639+
while queue:
640+
length = len(queue)
641+
in_list = []
642+
for _ in range(length):
643+
curnode = queue.pop(0)
644+
in_list.append(curnode.val)
645+
if curnode.left: queue.append(curnode.left)
646+
if curnode.right: queue.append(curnode.right)
647+
out_list.append(max(in_list))
648+
return out_list
649+
```
650+
630651
javascript代码:
631652

632653
```javascript
@@ -712,6 +733,42 @@ public:
712733
};
713734
```
714735

736+
python代码:
737+
738+
```python
739+
# 层序遍历解法
740+
classSolution:
741+
defconnect(self,root:'Node') ->'Node':
742+
ifnot root:
743+
returnNone
744+
queue= [root]
745+
while queue:
746+
n=len(queue)
747+
for iinrange(n):
748+
node= queue.pop(0)
749+
if node.left:
750+
queue.append(node.left)
751+
if node.right:
752+
queue.append(node.right)
753+
if i== n-1:
754+
break
755+
node.next= queue[0]
756+
return root
757+
758+
# 链表解法
759+
classSolution:
760+
defconnect(self,root:'Node') ->'Node':
761+
first= root
762+
while first:
763+
cur= first
764+
while cur:# 遍历每一层的节点
765+
if cur.left: cur.left.next= cur.right# 找左节点的next
766+
if cur.rightand cur.next: cur.right.next= cur.next.left# 找右节点的next
767+
cur= cur.next# cur同层移动到下一节点
768+
first= first.left# 从本层扩展到下一层
769+
return root
770+
```
771+
715772
##117.填充每个节点的下一个右侧节点指针II
716773

717774
题目地址:https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/
@@ -753,7 +810,48 @@ public:
753810
}
754811
};
755812
```
813+
python代码:
756814
815+
```python
816+
# 层序遍历解法
817+
class Solution:
818+
def connect(self, root: 'Node') -> 'Node':
819+
if not root:
820+
return None
821+
queue = [root]
822+
while queue: # 遍历每一层
823+
length = len(queue)
824+
tail = None # 每一层维护一个尾节点
825+
for i in range(length): # 遍历当前层
826+
curnode = queue.pop(0)
827+
if tail:
828+
tail.next = curnode # 让尾节点指向当前节点
829+
tail = curnode # 让当前节点成为尾节点
830+
if curnode.left : queue.append(curnode.left)
831+
if curnode.right: queue.append(curnode.right)
832+
return root
833+
834+
# 链表解法
835+
class Solution:
836+
def connect(self, root: 'Node') -> 'Node':
837+
if not root:
838+
return None
839+
first = root
840+
while first: # 遍历每一层
841+
dummyHead = Node(None) # 为下一行创建一个虚拟头节点,相当于下一行所有节点链表的头结点(每一层都会创建);
842+
tail = dummyHead # 为下一行维护一个尾节点指针(初始化是虚拟节点)
843+
cur = first
844+
while cur: # 遍历当前层的节点
845+
if cur.left: # 链接下一行的节点
846+
tail.next = cur.left
847+
tail = tail.next
848+
if cur.right:
849+
tail.next = cur.right
850+
tail = tail.next
851+
cur = cur.next # cur同层移动到下一节点
852+
first = dummyHead.next # 此处为换行操作,更新到下一行
853+
return root
854+
```
757855

758856
##总结
759857

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp