110.平衡二叉树(递归很难理解,思维很重要)
下面才是做二叉树的一种正确思维:

copy他人运行代码:
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def judgeDepth(self, root):
        # 节点为空,高度为 0
        if root == None:
            return 0
        # 递归计算左子树的最大高度
        leftHeight = self.judgeDepth(root.left)
        #  如果左子树不是平衡二叉树,肯定不是平衡二叉树
        if leftHeight == -1:
            return -1;
        # 递归计算右子树的最大高度
        rightHeight = self.judgeDepth(root.right)
        # 如果右子树不是平衡二叉树,肯定不是平衡二叉树
        if rightHeight == -1:
            return -1;
        # 若为平衡二叉树,平衡因子为 1、0、-1
        if abs(leftHeight - rightHeight) > 1:
            return -1
        else:
            # 二叉树的最大高度 = 子树的最大高度 + 1(1 是根节点)
            return max(leftHeight, rightHeight) + 1
    def isBalanced(self, root: TreeNode) -> bool:
        if self.judgeDepth(root) != -1:
            return True
        return False
 

自己看懂后,手撕运行代码:
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        if self.judge(root)==-1:
            return False
        else:
            return True
    def judge(self,root):
        if root==None:
            return 0
        left_height=self.judge(root.left)
        if left_height==-1:
            return -1
        right_height=self.judge(root.right)
        if right_height==-1:
            return  -1
        if abs(left_height-right_height)>1:
            return -1
        else:
            return 1+max(left_height,right_height)
 
 
 
257. 二叉树的所有路径 (有一个隐藏回溯)

模仿运行代码:
404.左叶子之和
copy代码:
class Solution:
    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        if not root:
            return 0
        left_left_leaves_sum = self.sumOfLeftLeaves(root.left)  # 左
        
        right_left_leaves_sum = self.sumOfLeftLeaves(root.right)  # 右
        
        cur_left_leaf_val = 0
        if root.left and not root.left.left and not root.left.right:
            cur_left_leaf_val = root.left.val
       
        return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum  # 中
 
  方法1(中序遍历)debug代码:
class Solution:
    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        if not root:
            return 0
        # print(root.left)
        print(root.val)
        left_left_leaves_sum = self.sumOfLeftLeaves(root.left)# 左
        print(root.val)
        if root.left and not root.left.left and not root.left.right:
            left_left_leaves_sum= root.left.val
        print(f"{root.val}+left", left_left_leaves_sum)
        # print(root.val)
        right_left_leaves_sum = self.sumOfLeftLeaves(root.right)  # 右
        print(root.val)
        print(f"{root.val}+right",right_left_leaves_sum )
        uu=left_left_leaves_sum + right_left_leaves_sum
        print(f"{root.val}+中",uu)
        return   uu # 中 
  Debug运行逻辑:
每个节点都要return uu(下图中红框)



 
方法2(后续遍历)Debug:

class Solution:
    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        if not root:
            return 0#高度为0
        print(root.val)
        left_sum=self.sumOfLeftLeaves(root.left)
        print(f"{root.val}+left", left_sum)
        right_sum=self.sumOfLeftLeaves(root.right)
        print(f"{root.val}+right", right_sum)
        fff=0
        if root.left and not root.left.left and not root.left.right:
            fff=root.left.val
        print(f"{root.val}+ff", fff)
        uu=fff+right_sum+left_sum
        print(f"uu+{fff}+{left_sum}+{right_sum}",uu)
        return uu 
 力扣方法3(前序遍历):
class Solution:
    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        if not root:
            return 0#高度为0
        fff = 0
        if root.left and not root.left.left and not root.left.right:
            fff=root.left.val
        print(root.val)
        left_sum=self.sumOfLeftLeaves(root.left)
        print(f"{root.val}+left", left_sum)
        right_sum=self.sumOfLeftLeaves(root.right)
        print(f"{root.val}+right", right_sum)
        print(f"{root.val}+ff", fff)
        uu=fff+right_sum+left_sum
        print(f"uu+{fff}+{left_sum}+{right_sum}",uu)
        return uu 

![[JAVA EE]创建Servlet——继承HttpServlet类笔记2](https://img-blog.csdnimg.cn/63dec33eb8fb43078f994eaccca3c93f.png)
![[前端基础]websocket协议](https://img-blog.csdnimg.cn/8d13bc87c4854523bf94b119db37b7e3.png)
















