Python一行代码实现功能

Python一行代码实现功能

Posted by BY on September 2, 2021

Python一行代码实现功能

快排

def func(nums):
    return func([i for i in nums[1:] if i<nums[0]]) + [nums[0]] + func([i for i in nums[1:] if i>=nums[0]])

前序遍历二叉树

def func(root):
    return [] if not root else [root.val] + func(root.left) + func(root.right)

中序遍历二叉树

def func(root):
    return [] if not root else func(root.left) + [root.val] + func(root.right)

后序遍历二叉树

def func(root):
    return [] if not root else func(root.left) + func(root.right) + [root.val]

翻转二叉树

def func(root):
    return root if not root else TreeNode(root.val, func(root.right), func(root.left))

遍历链表

def func(root):
    return [] if not root else [root.val] + func(root.next)