代码随想录算法训练营第十七天| LeetCode110.平衡二叉树、LeetCode257. 二叉树的所有路径、LeetCode404左叶子之和

110.平衡二叉树

题目描述: 110.平衡二叉树.

解法

递归
class Solution(object):
    def countHight(self,node):
        if not node:
            return 0
        left_height = self.countHight(node.left)
        right_height = self.countHight(node.right)
        if left_height == -1 or right_height == -1 or abs(left_height - right_height) > 1:
            return -1
        return max(left_height,right_height) + 1

        
    def isBalanced(self, root):
        return self.countHight(root) != -1

有的时候可以不用非要返回True或者False,使用-1也是可以的

257. 二叉树的所有路径

题目描述: 257. 二叉树的所有路径 .

解法

递归回溯
class Solution(object):
    def travel(self,node,path,res):
        path.append(node.val)
        if not node.left and not node.right:
            res.append("->".join(map(str,path)))
            return
        if node.left:
            self.travel(node.left,path,res)
            path.pop()
        if node.right:
            self.travel(node.right,path,res)
            path.pop()

    def binaryTreePaths(self, root):
        if not root:
            return []
        res = []
        path = []
        self.travel(root,path,res)
        return res

map的用法,map(a,b):对可迭代对象b进行函数a
本写法中的回溯,指的是我子节点的路径遍历结束,需要将子节点从当前path中取出来,当前节点则可以通过他的父节点取出来。

隐藏回溯
class Solution(object):
    def travel(self,node,path,res):
        path.append(node.val)
        if not node.left and not node.right:
            res.append('->'.join(map(str,path)))
        if node.left:
            self.travel(node.left,path[:],res)
        if node.right:
            self.travel(node.right,path[:],res)
        
    def binaryTreePaths(self, root):
        if not root:
            return []
        res = []
        self.travel(root,[],res)
        return res

每次传递的path都是新的切片,所以就不会导致原来的path发生改变。
或者是用path + “->”,这样每次传递的也不是原来的path,就不需要再进行pop操作修改了

迭代
class Solution(object):
    def binaryTreePaths(self, root):
        if not root:
            return []
        res = []
        path = [str(root.val)]
        st = [root]
        while st:
            node = st.pop()
            p = path.pop()
            if not (node.left or node.right):
                res.append(p)
            if node.right:
                st.append(node.right)
                path.append(p + "->" + str(node.right.val))
            if node.left:
                st.append(node.left)
                path.append(p + "->" + str(node.left.val))
        return res

多了一个path列表,当一个node被加入的时候,这个node的path也被加入其中,在pop每个节点之后,都要把path同时pop出来,那如果这个节点是叶节点,那就直接在结果中加入这个path。如果这个节点有孩子,那就将孩子的路径加入到path列表中,同时孩子也要加入st中。

404左叶子之和

题目描述: 404左叶子之和.

解法

递归
class Solution(object):
    def sumOfLeftLeaves(self, root):
        if not root:
            return 0
        if root.left and not root.left.right and not root.left.left:
            left_num = root.left.val
        else:
            left_num = self.sumOfLeftLeaves(root.left)
        right_num = self.sumOfLeftLeaves(root.right)
        return left_num + right_num

由于是要对叶节点访问计数,并且是只有左边的叶节点,因此最终要访问到的是左叶结点的父节点。
如果没有子节点,就要返回0。如果有左节点,且左节点没有子节点,那么就可以对左节点计数,否则就要进入到左右节点看情况了。

迭代法
class Solution(object):
    def sumOfLeftLeaves(self, root):
        if not root:
            return 0
        st = [root]
        sum = 0
        while st:
            node = st.pop()
            if node.left:
                if not node.left.left and not node.left.right:
                    sum += node.left.val
                else:
                    st.append(node.left)
            if node.right:
                st.append(node.right)
        return sum
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值