首页 > 其他分享 >leetcode-563-easy

leetcode-563-easy

时间:2023-01-04 19:34:38浏览次数:53  
标签:node right Tilt 563 subtree easy root leetcode left

Binary Tree Tilt

Given the root of a binary tree, return the sum of every tree node's tilt.

The tilt of a tree node is the absolute difference between the sum of all left subtree node values and all right subtree node values. 
If a node does not have a left child, then the sum of the left subtree node values is treated as 0. The rule is similar if the node does not have a right child.

Example 1:


Input: root = [1,2,3]
Output: 1
Explanation: 
Tilt of node 2 : |0-0| = 0 (no children)
Tilt of node 3 : |0-0| = 0 (no children)
Tilt of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3)
Sum of every tilt : 0 + 0 + 1 = 1
Example 2:


Input: root = [4,2,9,3,5,null,7]
Output: 15
Explanation: 
Tilt of node 3 : |0-0| = 0 (no children)
Tilt of node 5 : |0-0| = 0 (no children)
Tilt of node 7 : |0-0| = 0 (no children)
Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5)
Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7)
Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16)
Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15
Example 3:


Input: root = [21,7,14,1,1,2,2,3,3]
Output: 9
Constraints:

The number of nodes in the tree is in the range [0, 104].
-1000 <= Node.val <= 1000

思路一:用一个全局变量来储存所有的节点的倾斜值,递归计算树的每个节点

    private static int TILT_SUM = 0;

    public static int findTilt(TreeNode root) {
        TILT_SUM = 0;
        findTiltRecursive(root);

        return TILT_SUM;
        }

    public static int findTiltRecursive(TreeNode root) {
        if (root == null) {
        return 0;
        }

        int leftTilt = findTiltRecursive(root.left);
        int rightTilt = findTiltRecursive(root.right);
        int abs = Math.abs(leftTilt - rightTilt);
        TILT_SUM += abs;

        return leftTilt + rightTilt + root.val;
    }

标签:node,right,Tilt,563,subtree,easy,root,leetcode,left
From: https://www.cnblogs.com/iyiluo/p/17025808.html

相关文章

  • leetcode-349-easy
    IntersectionofTwoArraysGiventwointegerarraysnums1andnums2,returnanarrayoftheirintersection.Eachelementintheresultmustbeuniqueandyoum......
  • leetcode-350-easy
    IntersectionofTwoArraysIIGiventwointegerarraysnums1andnums2,returnanarrayoftheirintersection.Eachelementintheresultmustappearasmany......
  • leetcode-367-easy
    ValidPerfectSquareGivenapositiveintegernum,returntrueifnumisaperfectsquareorfalseotherwise.Aperfectsquareisanintegerthatisthesquar......
  • 【转载】SRS、EasyDarwin、ZLMediaKit、Monibuca对比分析
    声明转自liuzhen007的《SRS、EasyDarwin、ZLMediaKit、Monibuca对比分析》作者:liuzhen007链接:https://juejin.cn/post/6926739029496954888来源:稀土掘金著作权归作......
  • 【栈】LeetCode 20. 有效的括号
    题目链接20.有效的括号思路碰见左括号就入栈,碰见右括号就和检查栈顶括号是否配队。遍历完后还要检查栈是否为空,确定括号数量是合法的。代码classSolution{publ......
  • 【逆波兰表达式】【栈】LeetCode 150. 逆波兰表达式求值
    题目链接150.逆波兰表达式求值思路从左到右遍历tokens遇到数字便放入栈中,遇到运算符便弹出栈顶的两个数字进行运算。代码classSolution{publicintevalRPN(......
  • EasyAR4.0稀疏空间地图室内导航
    现有的AR室内导航,一种方案是利用运动跟踪实现,但是偏移较大。比较靠谱或者说能满足商业使用的还是稀疏空间地图。(ARCore管叫云锚点)实现效果如下:EasyAR稀疏云地图室内导航......
  • EasyAR4.0使用说明(五)----3D物体跟踪
    3D物体跟踪总体上是和平面图像跟踪差不多的,设置,包括程序控制,识别多个对象。区别只是目标对象的不同。总体说明3D物体跟踪对3D物体的纹理,也就是表面的图案的丰富程度是有要求......
  • 《Unity3D平台AR开发快速上手--基于EasyAR4.0》随书资源和相关说明
    新手《Unity3D平台AR开发快速上手–基于EasyAR4.0》上市了,现在京东和淘宝都有卖。书分为2个部分,第一部分是EasyAR4.0基础内容和使用,第二部分是利用EasyAR的稀疏空间地图做室......
  • 代码随想录day7 LeetCode 454. 四数相加 II 383. 赎金信 15.三数之和 18四数之和
    454.四数相加IIhttps://leetcode.cn/problems/4sum-ii/采用哈希表法,能比暴力法减少时间复杂度,先用两个数之和做出哈希表,再用剩下两数之和寻找哈希表中的数classSolu......