博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 563. Binary Tree Tilt
阅读量:6976 次
发布时间:2019-06-27

本文共 3016 字,大约阅读时间需要 10 分钟。

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     3Output: 1Explanation: Tilt of node 2 : 0Tilt of node 3 : 0Tilt of node 1 : |2-3| = 1Tilt 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 = Noneclass 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 = Noneclass 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 = Noneclass 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

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

 

转载地址:http://onupl.baihongyu.com/

你可能感兴趣的文章
jQuery学习---------认识事件处理
查看>>
Win7/Win8 系统下安装Oracle 10g 提示“程序异常终止,发生未知错误”的解决方法...
查看>>
获得PMP证书的这一年
查看>>
大型网站架构演变和知识体系
查看>>
jQuery EasyUI 表单插件 - Datebox 日期框
查看>>
要哭了,模拟器键盘一直不显示
查看>>
获取下个月的今天
查看>>
elasticsearch简介
查看>>
文件分区格式化及挂载
查看>>
Centos运行级别和开机过程
查看>>
Linux 装B之作酷炫小工具
查看>>
Citrix Avalon安装实验手册之一----Avalon概述及实验环境准备
查看>>
动态表单构建器——建造者模式
查看>>
Android 自动化测试
查看>>
MySQL 5.5 服务器变量详解(二)
查看>>
bootstrap table
查看>>
CentOS 7 yum 安装 MySQL5.7
查看>>
企业网络翻译官——DNS
查看>>
RocketMQ3.2.2生产者发送消息自动创建Topic队列数无法超过4个
查看>>
USG防火墙telnet实验
查看>>