When calling a function, add self.function name
list sort nums.sort()
**pre.next = l1 if l1 else l2 # Similar to C++ trinocular expression, l1 is true if l1 is true, otherwise l2 **
list list use
def convertBST(self, root: TreeNode) -> TreeNode:
stack = [(root,1)]
** sum = 0**
** while stack:**
** node, cmd = stack.pop()**
** if node is None:**
** continue**
** if cmd == 0:**
** sum += node.val**
** node.val = sum**
** else:**
** stack.append((node.left, 1))**
** stack.append((node, 0))**
** stack.append((node.right, 1))**
** return root**
Python defines global variables
self.sum =0
def convertBST(self, root: TreeNode) -> TreeNode:
if root == None:
** return**
** self.val = 0**
** def dfs(root):**
** if root == None:**
** return**
** root.right = dfs(root.right)**
** self.val += root.val**
** root.val = self.val**
** root.left = dfs(root.left)**
** return root**
** return dfs(root)**
python3 exchange
a, b = b, a # Similar to C++ swap operation
list simulation stack
self.stackB = []
if self.stackB: # Determine if it is empty
self.stackB[-1] # Take top()
self.stackB.append(x) # push operation
self.stackB.pop() # pop() operation
Create list
ary_len =len(prices)
dp= [0] * ary_len
import collections
d = collections.deque()
import collections
d = collections.deque()
d.append(1)
d.append(2)
print(d)
# Output: deque([1, 2])
d.appendleft(2)
d.clear()
x = d.pop()
x = d.popleft()
d.reverse()
Recommended Posts