解决思路 python

题目:二叉树镜像

def mirror_pre(root):
    ret=[]
    def travelsal(root):
        if root:
            ret.append(root.val)
            travelsal(root.right)
            travelsal(root.left)
    travelsal(root)
    return ret

题目:顺时针打印矩阵

def print_matrix(matrix):
    rows=len(matrix)
    cols=len(matrix[0])
    start=0
    ret=[]
    while start*2<rows and start*2<cols:
        print_circle(matrix, start, rows, cols, ret)
        start+=1
    return ret

def print_circle(matrix, start, rows, cols, ret):
    row=rows-start+1
    col=cols-start+1
    #left->right
    for i in range(start, col+1):
        ret.append(matrix[start][i])
    #top->bottom
    if start<row:
        for j in range(start+1, row+1):
            ret.append(matrix[r][col])
    #right->left
    if start<col and start<row:
        for k in range(start, col)[::-1]:
            ret.append(matrix[row][k])
    #bottom->top
    if start<col and start<row:
        for l in range(start, row)[::-1]:
            ret.append(matrix[l][col])
        

题目:包含min的栈

class my_stack(object):
    def __init__(self):
        self.stack=[]
        self.min=[]
        
    def push(self, val):
        self.stack.append(val)
        if self.min and self.min[-1]<val:
            self.min.append(self.min[-1])
        else:
            self.min.append(val)
    
    def pop(self):
        if self.stack:
            self.min.pop()
            return self.stack.pop()
        return None
    
    def min(self):
        return self.min[-1] if self.min else None

题目:从行往下打印二叉树

def bfs(tree):
    if not tree:
        return None
    stack=[tree]
    ret=[]
    while stack:
        node=stack.pop(0)
        ret.append(node.val)
        if node.left:
            stack.append(node.val)
        if node.right:
            stack.append(node.val)
    return ret

题目:后序遍历

def is_post_order(order):
    length = len(order)
    if length:
        root = order[-1]
        left = 0
        while order[left] < root:
            left += 1
        right = left
        while right < length - 1:
            if order[right] < root:
                return False
            right += 1
        left_ret = True if left == 0 else is_post_order(order[:left])
        right_ret = True if left == right else is_post_order(order[left:right])
        return left_ret and right_ret
    return False

题目:二叉树某值的路径

def find_path(tree, num):
    ret=[]
    if not tree:
        return ret
    path=[tree]
    sums=[tree.val]
    
    def dfs(tree):
        if tree.left:
            path.append(tree.left)
            sums.append(sums[-1]+tree.left.val)
            dfs(tree.left)
        if tree.right:
            path.append(tree.right)
            sums.append(sums[-1]+tree.right.val)
            dfs(tree.right)
        if not tree.left and not tree.right:
            if sums[-1] == num:
                ret.append([p.val for p in path])
        path.pop()
        sums.pop()
    dfs(tree)
    return ret

题目:链表的复制

class Solution(object):

    @staticmethod
    def clone_nodes(head):
        # 结点复制
        move = head
        while move:
            tmp = Node(move.val)
            tmp.next = move.next
            move.next = tmp
            move = tmp.next
        return head

    @staticmethod
    def set_nodes(head):
        # other指针设置
        move = head
        while move:
            m_next = move.next
            if move.other:
                m_next.other = move.other.next
            move = m_next.next
        return head

    @staticmethod
    def reconstruct_nodes(head):
        # 结点拆分
        ret = head.next if head else Node
        move = ret
        while head:
            head = move.next
            if head:
                move.next = head.next
                move = move.next
        return ret

    @staticmethod
    def clone_link(head):
        # 结果
        h = Solution.clone_nodes(head)
        h = Solution.set_nodes(h)
        ret = Solution.reconstruct_nodes(h)
        return ret

题目:二叉搜索树,双向链表

class Solution(object):

    @staticmethod
    def convert(tree):
        """结点转换"""
        if not tree:
            return None
        p_last = Solution.convert_nodes(tree, None)
        while p_last and p_last.left:  # 获取链表头结点
            p_last = p_last.left
        return p_last

    @staticmethod
    def convert_nodes(tree, last):
        if not tree:
            return None
        if tree.left:
            last = Solution.convert_nodes(tree.left, last)
        if last:
            last.right = tree
        tree.left = last
        last = tree
        if tree.right:
            last = Solution.convert_nodes(tree.right, last)
        return last

题目:字符串的排序

def my_permutation(s):
    str_set=[]
    ret=[]
    def permutation(string):
        for i in string:
            str_item=string.replace(i, ' ')
            str_set.append(i)
            if len(str_item)>0:
                permutation(str_item)
            else:
                ret.append(' '.join(str_set))
            str_set.pop()
        permutation(s)
        return ret
            

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值