LeetCode相关问题


鸡汤

LeetCode 刷题指南(一):为什么要刷题 对leetcode的评价非常客观


常用算法

并查集(Union-Find)算法介绍

LeetCode 解题报告(684,685)-并查集介绍及应用


遗留问题

725. Split Linked List in Parts

654. Maximum Binary Tree

题目大意: 给定无重复的数组。一棵最大树定义如下: 从数组中挑选最大的数字作为根 挑选左半数组中最大的数字作为左子树的根 挑选右半数组中最大的数字作为右子树的根 递归此过程。

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def constructMaximumBinaryTree(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        if not nums: return None
        maxn = max(nums)
        idx = nums.index(maxn)
        node = TreeNode(maxn)
        node.left = self.constructMaximumBinaryTree(nums[:idx])
        node.right = self.constructMaximumBinaryTree(nums[idx+1:])
        return node

[LeetCode]Maximum Binary Tree

617. Merge Two Binary Trees

[LeetCode]Merge Two Binary Trees

class Solution(object):
    def mergeTrees(self, t1, t2):
        """
        :type t1: TreeNode
        :type t2: TreeNode
        :rtype: TreeNode
        """
        if t1==None:
            # 左子树无内容,相当于0,直接返回右子树
            return t2
        if t2==None:
            # 右子树无内容,相当于0,直接返回左子树
            return t1
        t3=TreeNode(None)
        t3.left=self.mergeTrees(t1.left, t2.left)
        t3.right=self.mergeTrees(t1.right, t2.right)
        t3.val=t1.val+t2.val
        return t3