leecode第四周

树:

递归(110):https://leetcode.com/problems/balanced-binary-tree/description/

层次遍历(513):https://leetcode.com/problems/find-bottom-left-tree-value/description/

# 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 findBottomLeftValue(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        next_layer = [root]
        while next_layer:
            temp_next_layer = []
            layer_value = []
            for node in next_layer:
                if node.left:
                    temp_next_layer.append(node.left)
                if node.right:
                    temp_next_layer.append(node.right)
                layer_value.append(node.val)
                #print("layer_value:")
                #print(layer_value)
            next_layer = temp_next_layer
            #print("next_layer:")
            #print(next_layer)
        return layer_value[0]
        

前中后序遍历(144):https://leetcode.com/problems/binary-tree-preorder-traversal/description/

递归实现:

# 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 preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        return [root.val]+self.preorderTraversal(root.left)+self.preorderTraversal(root.right)

非递归实现:

# 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 preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        
        stack = [root]
        res = []
        while stack:
            cur = stack.pop()
            print(cur)
            print(stack)
            res.append(cur.val)
            if cur.right:
                stack.append(cur.right)
            if cur.left:
                stack.append(cur.left)
            
        return res
    

BST(230):https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/

# 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 kthSmallest(self, root, k):
        """
        :type root: TreeNode
        :type k: int
        :rtype: int
        """
        self.count = k
        self.res = 0
        def core(root):
            if root:
                #print(root)
                core(root.left)
                #print(self.count)
                self.count = self.count -1
                if self.count == 0:
                    self.res = root.val
                core(root.right)
        core(root)
        return self.res

Trie(208):https://leetcode.com/problems/implement-trie-prefix-tree/description/

https://blog.csdn.net/IOT_victor/article/details/88936762

 

图:

二分图(785):https://leetcode.com/problems/is-graph-bipartite/description/

https://blog.csdn.net/weixin_37251044/article/details/88829810

拓扑排序(207):https://leetcode.com/problems/course-schedule/description/

https://blog.csdn.net/IOT_victor/article/details/88936395

并查集(684):https://leetcode.com/problems/redundant-connection/description/

https://blog.csdn.net/qq_32424059/article/details/90207673
————————————————
版权声明:本文为CSDN博主「haimianjie2012」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/haimianjie2012/article/details/93344894

你可能感兴趣的:(深度学习)