Leetcode简单题N101、104、N107、N108、N110、111、N112

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jesmine_gu/article/details/85013474
树相关的部分掌握的很不好,题目很少有完整的思路。

(N) 101.对称二叉树

注:自己没想出来,打个标记

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def isSymmetric(self, root):
        def isSameTree(left, right):
            if not left and not right:
                return True
            elif left and right and left.val == right.val:
                l = isSameTree(left.left, right.right)
                r = isSameTree(right.left, left.right)
                return l and r
            else:
                return False

        if root is None:
            return True
        else:
            return isSameTree(root.left, root.right)
104. 二叉树的最大深度
class Solution(object):
    def maxDepth(self, root):
        if not root:
            return 0
        if root and not root.left and not root.right:
            return 1
        elif root.right or root.left:
            l = self.maxDepth(root.left)+1
            r = self.maxDepth(root.right)+1
            return max(l, r)
(N) 107. 二叉树的层次遍历 II

注:层次遍历思想

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def levelOrderBottom(self, root):
        if not root:
            return []
        queue = [root]
        res = []
        while queue:
            node = []
            for i in range(len(queue)):
                t = queue.pop(0)
                if t.left:
                    queue.append(t.left)
                if t.right:
                    queue.append(t.right)
                node.append(t.val)
            res.append(node)
        return res[::-1]

if __name__ == '__main__':
    t = Solution()
    tr = TreeNode(3)
    tr1 = TreeNode(9)
    tr2 = TreeNode(20)
    tr.left = tr1
    tr.right = tr2
    tr3 = TreeNode(15)
    tr2.left = tr3
    tr4 = TreeNode(7)
    tr2.right = tr4
    tt = t.levelOrderBottom(tr)
    print(tt)
(N) 108. 将有序数组转换为二叉搜索树
class Solution(object):
    def sortedArrayToBST(self, nums):
        if not nums:
            return None
        mid = len(nums) // 2
        root = TreeNode(nums[mid])
        root.left = self.sortedArrayToBST(nums[:mid])
        root.right = self.sortedArrayToBST(nums[mid+1:])
        return root

附:

前序遍历、中序遍历、后序遍历

def printf(self, root):
        if not root:
            return None
        else:
            # print的位置,先序遍历
            print(root.val)
            self.pre(root.left)
            # 中序遍历
            self.pre(root.right)
            # 后序遍历

 层次遍历输出

def printf(self, root):
        if not root:
            return []
        queue = [root]
        # temp = []
        while queue:
            t = queue.pop(0)
            print(t.val, end=" ")
            # temp.append(t.val)
            if t.left:
                queue.append(t.left)
            if t.right:
                queue.append(t.right)
         # return temp
(N) 110. 平衡二叉树

思想:计算左右子树深度作比较

class Solution(object):
    def isBalanced(self, root):
        def height(root):
            if root is None:
                return 0
            if not root.left and not root.right:
                return 1
            else:
                l = height(root.left)+1
                r = height(root.right)+1
                return max(l, r)
        if not root:
            return True
        if abs(height(root.left)-height(root.right)) > 1:
            return False
        else:
            return self.isBalanced(root.left) and self.isBalanced(root.right)
111. 二叉树的最小深度
class Solution(object):
    def minDepth(self, root):
        if not root:
            return 0
        elif not root.left:
            return self.minDepth(root.right)+1
        elif not root.right:
            return self.minDepth(root.left)+1
        else:
            return min(self.minDepth(root.left), self.minDepth(root.right))+1
(N) 112. 路径总和
class Solution(object):
    def hasPathSum(self, root, sum):
        if not root:
            return False
        # if sum == root.val and not root.right and not root.left:
        #     return True
        if not root.right and not root.left:
            return sum == root.val
        else:
            return self.hasPathSum(root.left, sum-root.val) and self.hasPathSum(root.right, sum-root.val)

猜你喜欢

转载自blog.csdn.net/jesmine_gu/article/details/85013474
N!
n
N*
今日推荐