当前位置:网站首页>Slope of binary tree
Slope of binary tree
2022-07-27 04:08:00 【Sharp blade CC】
563. The slope of the binary tree
The difficulty is simple 274
Give you the root node of a binary tree root , Calculate and return The whole tree Slope of .
A tree's The slope of the node The definition is , The sum of the nodes of the left subtree and the sum of the nodes of the right subtree of the node The absolute value of the difference . If there is no left subtree , The sum of the nodes of the left subtree is 0 ; It's the same without the right subtree . The slope of the empty node is 0 .
The whole tree The slope of a node is the sum of the slopes of all nodes .
Example 1:

Input :root = [1,2,3]
Output :1
explain :
node 2 Slope of :|0-0| = 0( No child node )
node 3 Slope of :|0-0| = 0( No child node )
node 1 Slope of :|2-3| = 1( The left subtree is the left child node , So and are 2 ; The right subtree is the right child node , So and are 3 )
Slope sum :0 + 0 + 1 = 1
Example 2:

Input :root = [4,2,9,3,5,null,7]
Output :15
explain :
node 3 Slope of :|0-0| = 0( No child node )
node 5 Slope of :|0-0| = 0( No child node )
node 7 Slope of :|0-0| = 0( No child node )
node 2 Slope of :|3-5| = 2( The left subtree is the left child node , So and are 3 ; The right subtree is the right child node , So and are 5 )
node 9 Slope of :|0-7| = 7( There is no left subtree , So and are 0 ; The right subtree is exactly the right child node , So and are 7 )
node 4 Slope of :|(3+5+2)-(9+7)| = |10-16| = 6( The left subtree value is 3、5 and 2 , And is 10 ; The right subtree value is 9 and 7 , And is 16 )
Slope sum :0 + 0 + 0 + 2 + 7 + 6 = 15
Example 3:

Input :root = [21,7,14,1,1,2,2,3,3]
Output :9
Tips :
- The number of nodes in the tree ranges from
[0, 104]Inside -1000 <= Node.val <= 1000
Ideas : Depth-first search (DFS)
First of all, understand the meaning of the title , We want to accumulate the absolute value of the difference between the sum of left subtree nodes and the sum of right subtree nodes of all nodes of the binary tree . therefore , We can use Depth-first search , When traversing each node , Accumulate the absolute value of the difference between the sum of the left subtree nodes and the sum of the right subtree nodes , And return the sum of the nodes of the tree with it as the root node .
- So let's create one Global variables sum Used to accumulate the sum of the absolute values of each node , Then create a sub function , To subfunction _find() De recursion .
- use left and right Recursively get the values of the left subtree and the right subtree respectively , And then use sum Plus the difference between their absolute values .
- Return to root The sum of the nodes of the tree as the root node left + right + root->val
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
int sum = 0;
int _find(TreeNode* root)
{
if(root == nullptr)
return 0;
int left = _find(root->left);
int right = _find(root->right);
sum += abs(left - right);
return root->val + left + right;
}
int findTilt(TreeNode* root) {
_find(root);
return sum;
}
};
Complexity analysis
Time complexity :O(n), among n Is the total number of nodes in the binary tree
Spatial complexity :O(n)
边栏推荐
猜你喜欢

Lixia action | Yuanqi Digitalization: existing mode or open source innovation?

288页18万字智能化校园总体设计 目录

大咖说·图书分享|精益产品开发:原则、方法与实施

2022年危险化学品经营单位主要负责人复训题库及答案

「Gonna Be Alright 会好的」数藏现已开售!感受艺术家的心灵共鸣

356页14万字高端商业办公综合楼弱电智能化系统2022版

Subject 3: Jinan Zhangqiu line 5

VR全景制作在家装行业是谈单利器?这是为什么呢?

C how to set different text watermarks for each page of word

3381. Mobile keyboard (Tsinghua University postgraduate entrance examination machine test question)
随机推荐
Article main content extraction software [based on NLP technology]
A. YES or YES?
Subject 3: Jinan Zhangqiu line 6
Subject 3: Jinan Zhangqiu line 2
Subject 3: Jinan Zhangqiu line 5
第五届强网杯全国网络安全挑战赛 题目复现(有题目附件,详解)
真正意义上的数字零售应当具有更加丰富的内涵和意义
开机启动流程及营救模式
Kotlin中lateinit和lazy的原理区别是什么
MySQL: understand the basic knowledge of MySQL and computer
Golang jwt跨域鉴权
分享当下人生——一个高中毕业生在中央电视台的六星期实习经历
Parallel desktop startup virtual machine "operation failed" problem solution
B. ICPC Balloons
leetcode:433. 最小基因变化
C # using sqlsugar updatable system to report invalid numbers, how to solve it? Ask for guidance!
0726~简历梳理面试总结
线上一个隐匿 Bug 的复盘
零基础小白也能懂的 Redis 数据库,手把手教你易学易用!
JVM原理简介