Skip to content

Commit 0487021

Browse files
committed
366_Find_Leaves_of_Binary_Tree
369_Plus_One_Linked_List
1 parent b6cf5d4 commit 0487021

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@
109109
[Python](https://github.com/qiyuangong/leetcode/blob/master/python/346_Moving_Average_from_Data_Stream.py) | fix-sized queue or dequeue, O(1) and O(n) |
110110
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/351_Android_Unlock_Patterns.py) | Backtracking, O(n!) and O(n) |
111111
| 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/359_Logger_Rate_Limiter.py) | 1. hash which stores the latest timestamp, O(1) and O(n)<br>2. Using Priority queue to remove older logs, O(n) and O(n) |
112+
| 366 | [Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/366_Find_Leaves_of_Binary_Tree.py) | 1. Set or hash to check leaft, O(n^2) and O(n)<br>2. Recursively check level and return them, O(n) and O(n)|
112113
| 368 | [Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/368_Largest_Divisible_Subset.py) | Sort and generate x subset with previous results, O(n^2) and O(n^2) |
114+
| 369 | [Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/369_Plus_One_Linked_List.py) | 1. Stack or list that store the list, O(n) and O(n)<br>2. Two points, the first to the tail, the second to the latest place that is not 9, O(n) and O(1) |
113115
| 370 | [Range Addition](https://leetcode.com/problems/range-addition/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/370_Range_Addition.py) | Interval problem with cumulative sums, O(n + k) and O(n) |
114116
| 384 | [Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/384_Shuffle_an_Array.py) | Fisher–Yates shuffle, O(n) and O(n) |
115117
| 388 | [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/388_Longest_Absolute_File_Path.py) | Store last length and rindex, O(n) and O(n) |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
9+
class Solution(object):
10+
# def findLeaves(self, root):
11+
# """
12+
# :type root: TreeNode
13+
# :rtype: List[List[int]]
14+
# """
15+
# res = []
16+
# if root is None:
17+
# return []
18+
# stack = [root]
19+
# check_set = set()
20+
# while len(stack) > 0:
21+
# curr = stack.pop()
22+
# check_set.add(curr)
23+
# if curr.left:
24+
# stack.append(curr.left)
25+
# if curr.right:
26+
# stack.append(curr.right)
27+
# while len(check_set) > 0:
28+
# curr = []
29+
# for node in check_set:
30+
# if (node.left is None or node.left not in check_set) and\
31+
# (node.right is None or node.right not in check_set):
32+
# curr.append(node)
33+
# res.append([node.val for node in curr])
34+
# for node in curr:
35+
# check_set.remove(node)
36+
# return res
37+
38+
def findLeaves(self, root):
39+
res = []
40+
self.findLeaves_helper(root, res)
41+
return res
42+
43+
def findLeaves_helper(self, node, res):
44+
if node is None:
45+
return -1
46+
level = 1 + max(self.findLeaves_helper(node.left, res), self.findLeaves_helper(node.right, res))
47+
if len(res) < level + 1:
48+
res.append([])
49+
res[level].append(node.val)
50+
return level

python/369_Plus_One_Linked_List.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
8+
class Solution(object):
9+
def plusOne(self, head):
10+
"""
11+
:type head: ListNode
12+
:rtype: ListNode
13+
"""
14+
dummy = ListNode(0)
15+
dummy.next = head
16+
place_stop, tail = dummy, dummy
17+
# find the tail
18+
while tail.next is not None:
19+
tail = tail.next
20+
if tail.val != 9:
21+
place_stop = tail
22+
if tail.val != 9:
23+
# done
24+
tail.val += 1
25+
else:
26+
# not yet
27+
place_stop.val += 1
28+
place_stop = place_stop.next
29+
# set all node behind this place to zero
30+
while place_stop is not None:
31+
place_stop.val = 0
32+
place_stop = place_stop.next
33+
if dummy.val == 0:
34+
return dummy.next
35+
return dummy

0 commit comments

Comments
 (0)