Skip to content

Commit a7f293a

Browse files
committed
solved invert Binary tree
1 parent 8baae43 commit a7f293a

File tree

3 files changed

+92
-13
lines changed

3 files changed

+92
-13
lines changed

0ne.ipynb

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 71,
5+
"execution_count": 39,
66
"metadata": {},
77
"outputs": [],
88
"source": [
@@ -11,33 +11,62 @@
1111
" self.val = val\n",
1212
" self.next = next\n",
1313
"\n",
14-
"\n",
1514
"a = ListNode(-1, ListNode(2, ListNode(8)))\n",
16-
"b = ListNode(3, ListNode(4, ListNode(7)))"
15+
"b = ListNode(-3, ListNode(4, ListNode(7)))"
1716
]
1817
},
1918
{
2019
"cell_type": "code",
21-
"execution_count": 84,
20+
"execution_count": 40,
2221
"metadata": {},
2322
"outputs": [
2423
{
2524
"name": "stdout",
2625
"output_type": "stream",
2726
"text": [
28-
"('a', 1)\n",
29-
"('b', 2)\n"
27+
"[-3, -1, 2, 4, 7, 8]\n"
3028
]
29+
},
30+
{
31+
"data": {
32+
"text/plain": [
33+
"<__main__.ListNode at 0x1887c6f4b80>"
34+
]
35+
},
36+
"execution_count": 40,
37+
"metadata": {},
38+
"output_type": "execute_result"
3139
}
3240
],
3341
"source": [
34-
"a = {\n",
35-
" 'a': 1,\n",
36-
" 'b': 2\n",
37-
"}\n",
38-
"\n",
39-
"for node in a.items():\n",
40-
" print(node)"
42+
"class Solution:\n",
43+
" def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:\n",
44+
" dummy = ListNode()\n",
45+
" tail = dummy\n",
46+
" \n",
47+
" while l1 and l2:\n",
48+
" if l1.val < l2.val:\n",
49+
" tail.next = l1\n",
50+
" l1 = l1.next\n",
51+
" else:\n",
52+
" tail.next = l2\n",
53+
" l2 = l2.next\n",
54+
" tail = tail.next\n",
55+
" \n",
56+
" if l1:\n",
57+
" tail.next = l1\n",
58+
" elif l2:\n",
59+
" tail.next = l2\n",
60+
" \n",
61+
" arr = []\n",
62+
" node = dummy.next\n",
63+
" while node:\n",
64+
" arr.append(node.val)\n",
65+
" node = node.next\n",
66+
" print(arr)\n",
67+
" return dummy.next\n",
68+
" \n",
69+
"Solution.mergeTwoLists(0, a,b)"
4170
]
4271
}
4372
],

21-Merge-Two-Sorted-Lists.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class ListNode:
2+
def __init__(self, val=0, next=None) -> None:
3+
self.val = val
4+
self.next = next
5+
6+
a = ListNode(-1, ListNode(2, ListNode(8)))
7+
b = ListNode(-3, ListNode(4, ListNode(7)))
8+
9+
from typing import Optional
10+
11+
class Solution:
12+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
13+
dummy = ListNode()
14+
tail = dummy
15+
16+
while list1 and list2:
17+
if list2.val > list1.val:
18+
tail.next = list1
19+
list1 = list1.next
20+
tail = tail.next
21+
else:
22+
tail.next = list2
23+
list2 = list2.next
24+
tail = tail.next
25+
26+
if list1:
27+
tail.next = list1
28+
elif list2:
29+
tail.next = list2
30+
31+
return dummy.next
32+

226-Invert-Binary-Tree.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class TreeNode:
2+
def __init__(self, val=0, left=None, right=None):
3+
self.val = val
4+
self.left = left
5+
self.right = right
6+
7+
from typing import Optional
8+
9+
class Solution:
10+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
11+
if root is None: return None
12+
13+
root.left, root.right = root.right, root.left
14+
15+
self.invertTree(root.right)
16+
self.invertTree(root.left)
17+
18+
return root

0 commit comments

Comments
 (0)