@@ -98,7 +98,7 @@ class Solution:
98
98
out_list= []
99
99
100
100
while quene:
101
- length= len (queue)# 这里一定要先求出队列的长度,不能用range(len(queue)),因为queue长度是变化的
101
+ length= len (queue)
102
102
in_list= []
103
103
for _in range (length):
104
104
curnode= queue.pop(0 )# (默认移除列表最后一个元素)这里需要移除队列最头上的那个
@@ -627,6 +627,27 @@ public:
627
627
}
628
628
};
629
629
```
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
+
630
651
javascript代码:
631
652
632
653
``` javascript
@@ -712,6 +733,42 @@ public:
712
733
};
713
734
```
714
735
736
+ python代码:
737
+
738
+ ``` python
739
+ # 层序遍历解法
740
+ class Solution :
741
+ def connect (self ,root :' Node' ) ->' Node' :
742
+ if not root:
743
+ return None
744
+ queue= [root]
745
+ while queue:
746
+ n= len (queue)
747
+ for iin range (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
+ class Solution :
760
+ def connect (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
+
715
772
##117.填充每个节点的下一个右侧节点指针II
716
773
717
774
题目地址:https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/
@@ -753,7 +810,48 @@ public:
753
810
}
754
811
};
755
812
```
813
+ python代码:
756
814
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
+ ```
757
855
758
856
##总结
759
857