Skip to content

Commit e54983d

Browse files
14 may
1 parent 28322ea commit e54983d

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

Challenges/2021/May-LeetCoding-Challenge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ None
3030
|1423.|[Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)|[Python](/Medium/1423.MaximumPointsYouCanObtainfromCards.py)|Medium|
3131
|304.|[Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)|[Python](/Medium/304.RangeSumQuery2D-Immutable(bruteforce2).py)|Medium|
3232
|816.|[Ambiguous Coordinates](https://leetcode.com/problems/ambiguous-coordinates/)|~~Python~~|Medium|
33+
|114.|[PFlatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)|[Python](/Medium/114.FlattenBinaryTreetoLinkedList.py)|Medium|
3334

3435
## License
3536
The code is open-source and licensed under the [MIT License](/LICENSE).
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
Given the root of a binary tree, flatten the tree into
3+
a "linked list":
4+
- The "linked list" should use the same TreeNode
5+
class where the right child pointer points to the
6+
next node in the list and the left child pointer
7+
is always null.
8+
- The "linked list" should be in the same order as
9+
a pre-order traversal of the binary tree.
10+
11+
Example:
12+
Input: root = [1,2,5,3,4,null,6]
13+
Output: [1,null,2,null,3,null,4,null,5,null,6]
14+
15+
Example:
16+
Input: root = []
17+
Output: []
18+
19+
Example:
20+
Input: root = [0]
21+
Output: [0]
22+
23+
Constraints:
24+
- The number of nodes in the tree is in the range
25+
[0, 2000].
26+
- -100 <= Node.val <= 100
27+
28+
Follow up: Can you flatten the tree in-place (with O(1)
29+
extra space)?
30+
'''
31+
#Difficulty: Medium
32+
#225 / 225 test cases passed.
33+
#Runtime: 40 ms
34+
#Memory Usage: 15.2 MB
35+
36+
#Runtime: 40 ms, faster than 45.64% of Python3 online submissions for Flatten Binary Tree to Linked List.
37+
#Memory Usage: 15.2 MB, less than 45.95% of Python3 online submissions for Flatten Binary Tree to Linked List.
38+
39+
# Definition for a binary tree node.
40+
# class TreeNode:
41+
# def __init__(self, val=0, left=None, right=None):
42+
# self.val = val
43+
# self.left = left
44+
# self.right = right
45+
class Solution:
46+
47+
def flatten(self, root: TreeNode) -> None:
48+
"""
49+
Do not return anything, modify root in-place instead.
50+
"""
51+
values = []
52+
self.dfs(root, values)
53+
for value in values[1:]:
54+
root.right = TreeNode(value)
55+
root.left = None
56+
root = root.right
57+
58+
def dfs(self, root, values):
59+
if not root:
60+
return
61+
values.append(root.val)
62+
self.dfs(root.left, values)
63+
self.dfs(root.right, values)

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python solutions of LeetCode problems.
22
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
3-
![Problems Solved](https://img.shields.io/badge/problems%20solved-550%2F1665-orange)&nbsp;
3+
![Problems Solved](https://img.shields.io/badge/problems%20solved-551%2F1665-orange)&nbsp;
44
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
55
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
66
<br><br>
@@ -23,7 +23,7 @@ In this repository provided my Python solutions of LeetCode problems.
2323
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
2424
- [March LeetCoding Challenge](/Challenges/2021/March-LeetCoding-Challenge.md) - 23/31
2525
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 23/30
26-
- [May LeetCoding Challenge](/Challenges/2021/May-LeetCoding-Challenge.md) - 5/31
26+
- [May LeetCoding Challenge](/Challenges/2021/May-LeetCoding-Challenge.md) - 6/31
2727
<br><br>
2828
## Solutions
2929
*P.S. If you like this, please leave me a star.*
@@ -106,6 +106,7 @@ In this repository provided my Python solutions of LeetCode problems.
106106
|110.|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)|[Python](/Easy/110.BalancedBinaryTree.py)|Easy|`DFS`, `Recursion`|
107107
|111.|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|[Python](/Easy/111.MinimumDepthofBinaryTree.py)|Easy|`Binary Tree`, `Recursion`|
108108
|112.|[Path Sum](https://leetcode.com/problems/path-sum/)|[Python](/Easy/112.PathSum.py)|Easy|`Binary Tree`, `Recursion`|
109+
|114.|[PFlatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)|[Python](/Medium/114.FlattenBinaryTreetoLinkedList.py)|Medium|`DFS`, preorder|
109110
|116.|[Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)|[Python](/Medium/116.PopulatingNextRightPointersinEachNode.py)|Medium|`BFS`|
110111
|117.|[Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)|[Python](/Medium/117.PopulatingNextRightPointersinEachNodeII.py)|Medium|`BFS`, same as 116|
111112
|118.|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Python](https://github.com/YuriSpiridonov/LeetCode/blob/master/Easy/118.PascalsTriangle.py)|Easy|

0 commit comments

Comments
 (0)