代码随想录刷题营Day17(513:找树左下角的值,112:路径总和,113:路径总和2,106,105:从中和后或前和中构造二叉树)

513:找树左下角的值

这个题目首先要理解对题目,找左下角的值不是说只找左子树的左孩子,而是最后一层的最左边的那个节点的值,所以已知保持向左遍历就对了,中前后序都是可以的。这道题还要记录depth,一定要找到最后一层的depth。通过这个题还学会了nonlocal的使用,使用nonlocal,可以改变一个闭包里的值,二与gobal的区别就是gobal是全局的。

sum0=100
def outer(){
sum=0
	def inner(){
		global sum0
		nonlocal sum
		sum=sum+1
	}
}
当我要在inner里面改变sum的值时,需要先nonlocal一下,当我要改变sum0的值,我要使用global进行声明一下。outer{}就相当于是一个闭包。这里理解为一个局部吧。

具体代码如下:

class Solution:
    def findBottomLeftValue(self,root):
        max_value=float("-inf")
        res=0
        depth=0
        if not root:
            return res
        def Traversal(note,depth):
            nonlocal max_value,res
            if not note.left and not note.right:
                if depth>max_value:
                    max_value=depth
                    res=note.val
            if note.left:
                depth=depth+1
                Traversal(note.left,depth)
                depth=depth-1
            if note.right:
                depth=depth+1
                Traversal(note.right,depth)
                depth=depth-1
        Traversal(root,depth)
        return res

112:路经总和

路经总和这个题,根据题意,它不需要把所有的节点都遍历一遍,所以需要找到一个路径满足targetSum的值即可。
终止条件就是:找到一个叶子节点并且这个路径使得count0,就返回True。而找到一个叶子节点但是经过的路径没有办法使得count0,那就返回False。

class Solution:
    def hasPathSum(self, root, targetSum: int) -> bool:
        def Traversal(node,count):
            if not node.left and not node.right and count==0:
                return True
            if not node.left and not node.right and count!=0:
                return False
            if node.left:
                count=count-node.left.val
                if Traversal(node.left,count):
                    return True
                count=count+node.left.val
            if node.right:
                count=count-node.right.val
                if Traversal(node.right,count):
                    return True
                count=count+node.right.val
            return False
        count=targetSum
        if not root:
            return False
        else:
            return Traversal(root,count-root.val)

值得注意的是,传入Traversal的值,一定是除去根节点的,这个一开始没注意。


113:路经总和2

class Solution:
    def pathSum(self, root, targetSum: int) -> bool:
        if not root:
            return []
        path=[]
        path.append(root.val)
        res=[]
        def Traversal(node,count,path):
            if not node.left and not node.right and count==0:
                res.append(path[:])
                return
            if node.left:
                count=count-node.left.val
                path.append(node.left.val)
                Traversal(node.left,count,path)
                count=count+node.left.val
                path.pop()
            if node.right:
                count=count-node.right.val
                path.append(node.right.val)
                Traversal(node.right,count,path)
                count=count+node.right.val
                path.pop()
        count=targetSum
        Traversal(root,count-root.val,path)
        return res

哈哈,自己竟然刷过了,就提交了两次,果然这几天二叉树没白学!


106:从中序和后序数组中构造一棵二叉树

没想到这个题要用递归大法去做。跟着视频的思路,写了一下,觉得如果要把这个题做出来,首先要理清楚。第一,返回值的理解;第二,中序数组和后序数组的切分一定要准,边界值把握好。因为这两个数组在“终止条件”中,是动态变化的。例如,需要不断地获取切分的后序数组的最后那一个节点,为中节点,然后用这个节点,从中序数组中进行查找这个节点的index值,从而把左数组和右数组分出来。通过找到的这两个左数组和右数组,就可以在后序遍历中把左数组和右数组切分出来,然后去除这两个数组的最后一个值,就是这两个数组的中间节点。

class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:

        def Traversal(inorder,postorder):
            if len(postorder)==0:
                return None
            root=TreeNode(postorder[-1])
            if len(postorder)==1:
                return root
            for index in range(len(inorder)):
                if inorder[index]==postorder[-1]:
                    break
            inorder_left=inorder[0:index]
            inorder_right=inorder[index+1:]
            postorder_left=postorder[:len(inorder_left)]
            postorder_right=postorder[len(inorder_left):len(postorder)-1]
            root.left=Traversal(inorder_left,postorder_left)
            root.right=Traversal(inorder_right,postorder_right)
            return root
        result=Traversal(inorder,postorder)
        return result
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值