leetcode 563. Binary Tree Tilt

Given a binary tree, return the tilt of the whole tree.

The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0.

The tilt of the whole tree is defined as the sum of all nodes' tilt.

Example:

Input: 
         1
       /   \
      2     3
Output: 1
Explanation: 
Tilt of node 2 : 0
Tilt of node 3 : 0
Tilt of node 1 : |2-3| = 1
Tilt of binary tree : 0 + 0 + 1 = 1

Note:

  1. The sum of node values in any subtree won't exceed the range of 32-bit integer.
  2. All the tilt values won't exceed the range of 32-bit integer.

 

感觉将先序遍历,后序遍历都折腾了。注意nonlocal

复制代码
# 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 findTilt(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.build_sum(root)
        self.build_tilt(root)
        sum_val = 0
        def dfs(node):
            if not node: return 0
            nonlocal sum_val
            sum_val += node.val
            dfs(node.left)
            dfs(node.right)        
        dfs(root)
        return sum_val
    
    def build_sum(self, node):
        if not node: return 0
        l_val = self.build_sum(node.left)
        r_val = self.build_sum(node.right)
        node.val += l_val+r_val
        return node.val
    
    def build_tilt(self, node):
        if not node: return 0
        node.val = abs((node.left and node.left.val or 0) - (node.right and node.right.val or 0))
        self.build_tilt(node.left)
        self.build_tilt(node.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 findTilt(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.sum_val = 0
        self.build_sum(root)
        return self.sum_val
        
    
    def build_sum(self, node):
        if not node: return 0, 0
        l_sum_val, l_tilt_val  = self.build_sum(node.left)
        r_sum_val, r_tilt_val = self.build_sum(node.right)        
        tilt_val = abs(l_sum_val-r_sum_val) 
        self.sum_val += tilt_val
        return (node.val+l_sum_val+r_sum_val, tilt_val)            
复制代码

再度精简:

复制代码
# 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 findTilt(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.sum_val = 0
        self.build_sum(root)
        return self.sum_val
            
    def build_sum(self, node):
        if not node: return 0
        l_sum_val  = self.build_sum(node.left)
        r_sum_val = self.build_sum(node.right)        
        self.sum_val += abs(l_sum_val-r_sum_val) 
        return node.val+l_sum_val+r_sum_val           
复制代码

后序遍历的迭代解法不写了,还不能一次性写对。。。

 

posted @   bonelee  阅读(203)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示