We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bca62bd commit 8336aecCopy full SHA for 8336aec
3 files changed
.idea/workspace.xml
Target Offer/二叉树的镜像.py
@@ -43,6 +43,22 @@ def Mirror2(self, root):
43
if tree.right:
44
stackNode.append(tree.right)
45
nodeNum += 1
46
+ # 非递归实现
47
+ def MirrorNoRecursion(self, root):
48
+ if root == None:
49
+ return
50
+ nodeQue = [root]
51
+ while len(nodeQue) > 0:
52
+ curLevel, count = len(nodeQue), 0
53
+ while count < curLevel:
54
+ count += 1
55
+ pRoot = nodeQue.pop(0)
56
+ pRoot.left, pRoot.right = pRoot.right, pRoot.left
57
+ if pRoot.left:
58
+ nodeQue.append(pRoot.left)
59
+ if pRoot.right:
60
+ nodeQue.append(pRoot.right)
61
62
63
pNode1 = TreeNode(8)
64
pNode2 = TreeNode(6)
Target Offer/反转链表.py
@@ -24,6 +24,15 @@ def ReverseList(self, pHead):
24
pPrev = pNode
25
pNode = pNext
26
return pReversedHead
27
+ # 递归实现反转链表
28
+ def ReverseListRec(self, pHead):
29
+ if not pHead or not pHead.next:
30
+ return pHead
31
+ else:
32
+ pReversedHead = self.ReverseList(pHead.next)
33
+ pHead.next.next = pHead
34
+ pHead.next = None
35
+ return pReversedHead
36
37
node1 = ListNode(10)
38
node2 = ListNode(11)
0 commit comments