Skip to content

Commit 8336aec

Browse files
添加新的方法
1 parent bca62bd commit 8336aec

3 files changed

Lines changed: 51 additions & 25 deletions

File tree

.idea/workspace.xml

Lines changed: 26 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Target Offer/二叉树的镜像.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ def Mirror2(self, root):
4343
if tree.right:
4444
stackNode.append(tree.right)
4545
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+
return
4662

4763
pNode1 = TreeNode(8)
4864
pNode2 = TreeNode(6)

Target Offer/反转链表.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ def ReverseList(self, pHead):
2424
pPrev = pNode
2525
pNode = pNext
2626
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
2736

2837
node1 = ListNode(10)
2938
node2 = ListNode(11)

0 commit comments

Comments
 (0)