啊啊 啊 好久没写了 我发现一件事情一懒 件件事情都会变懒。元旦前把这一年的结了吧 107 - 113 还送个103?

103 :锯齿形层序遍历 

不谈了 层序会 这个也没啥难得 

加了奇 偶层的判断

    def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
        if root == None:
            return []
        que = []
        rel = []
        curlen = 1
        curlevel = 1
        que.append(root)
        while que != []:
            curlev = []
            nextlen = 0
            for i in range(curlen):
                temp = que[0]
                curlev.append(temp.val)
                if temp.left:
                    nextlen += 1
                    que.append(temp.left)
                if temp.right:
                    nextlen += 1
                    que.append(temp.right)
                del que[0]
            curlen = nextlen
            if curlevel % 2 == 0:
                rel.append(curlev[::-1])
            else:
                rel.append(curlev[:])
            curlevel += 1
        return rel

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/solution/jiu-jia-liao-yi-ju-hua-by-yizhu-jia-pprc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

107:层序遍历 不多说 

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if root == None:
            return []
        que = []
        rel = []
        curlen = 1
        que.append(root)
        while que != []:
            curlev = []
            nextlen = 0
            for i in range(curlen):
                temp = que[0]
                curlev.append(temp.val)
                if temp.left:
                    nextlen += 1
                    que.append(temp.left)
                if temp.right:
                    nextlen += 1
                    que.append(temp.right)
                del que[0]
            curlen = nextlen
            rel.append(curlev[:])
        return rel[::-1]

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/solution/-by-yizhu-jia-7lgo/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

108: 有序数组转换为二叉搜索树 

用递归
每次挑选中间的作为根结点
左边的一半作为左子树
右边的一半作为右子树

 

class Solution:
    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        def buildtree(start,end):
            if start >= end:
                return None
            root = TreeNode(val = nums[(start+end)//2])
            root.left = buildtree(start,(start+end)//2)
            root.right = buildtree((start+end)//2+1,end)
            return root
        return buildtree(0,len(nums))

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/solution/bu-rong-yi-er-cha-shu-de-ti-wo-ye-neng-j-tuyj/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

109 : 链表转换为二叉搜索树 

把链表转为列表 然后上一题 我无语

class Solution:
    def sortedListToBST(self, head: ListNode) -> TreeNode:

        def lnode2list(ListNode):
            rel=[]
            while ListNode!= None:
                rel.append(ListNode.val)
                ListNode = ListNode.next
            return rel
        nums = lnode2list(head)
        def buildtree(start,end):
            if start >= end:
                return None
            root = TreeNode(val = nums[(start+end)//2])
            root.left = buildtree(start,(start+end)//2)
            root.right = buildtree((start+end)//2+1,end)
            return root
        return buildtree(0,len(nums))

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/solution/hao-jia-huo-wo-zhi-jie-hao-ba-jia-shang-rp20l/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

110: 平衡二叉树 的判断  信手拈来

递归从下往上累计高度
用flag进行剪枝
值得注意的是 我是如果flag变为false就结束 但是这样不return一个值就会报错
但是当flag是false时 返回什么都无关紧要了 随便写了个0

class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        flag = True
        def post(root):
            nonlocal flag
            if root == None:
                return 0
            if flag:
                highl = post(root.left)
                highr = post(root.right)
                if abs(highl-highr) >1:
                    flag = False
                return max(highl,highr)+1
            else:
                return 0
        post(root)
        return flag

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/balanced-binary-tree/solution/chao-guo-da-bu-fen-wan-jia-by-yizhu-jia-fts0/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

111 :二叉树最小深度 

 


我讨厌递归 空间总是被打败  可是递归写起来好简单 呜呜呜

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        mindepth = 100001
        def pre(root,high):
            nonlocal mindepth
            if root.left == None and root.right == None:
                if high < mindepth:
                    mindepth = high
                return 0
            if root.left:
                pre(root.left,high+1)
            if root.right:
                pre(root.right,high+1)
        if root == None:
            return 0
        pre(root,1)
        return mindepth

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/solution/wo-tao-yan-di-gui-kong-jian-zong-shi-bei-8vcw/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

112 : 路径总和  

跟求高度差不多  遍历时把当前结点穿进去 当遇到叶子结点时就进行和的判断

class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        flag = False
        def pre(root,cursum):
            nonlocal flag
            if root.left == None and root.right == None:
                if root.val + cursum == targetSum:
                    flag = True
                return
            if not flag :
                if root.left:
                    pre(root.left,cursum+root.val)
                if root.right:
                    pre(root.right,cursum+root.val)
        if root == None:
            return flag
        pre(root,0)
        return flag

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/path-sum/solution/gen-qiu-gao-du-chai-bu-duo-by-yizhu-jia-dwxy/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

113 路径总和2

 前序遍历 是我的爱  递归递到花盛开

class Solution:
    def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
        rel = []
        def pre(root,cursum,curpath):
            curpath.append(root.val)
            if root.left == None and root.right == None:
                if root.val + cursum == targetSum:
                    rel.append(curpath[:])
                curpath.pop()
                return
            if root.left:
                pre(root.left,cursum+root.val,curpath)
            if root.right:
                pre(root.right,cursum+root.val,curpath)
            curpath.pop()
        if root == None:
            return rel
        pre(root,0,[])
        return rel

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/path-sum-ii/solution/qian-xu-bian-li-shi-wo-de-ai-by-yizhu-ji-6e2i/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

posted @ 2021-12-31 16:28  yi术家  阅读(30)  评论(0编辑  收藏  举报