Skip to content

Commit d94340e

Browse files
committed
563
1 parent 72d1230 commit d94340e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
8888
| # | Title | Solution | Time | Space | Difficulty |Tag| Note|
8989
|-----|-------| -------- | ---- | ------|------------|---|-----|
9090
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/solutions)| [Python [Yu]](./tree/Yu/108.py) | _O(N)_| _O(N)_ | Easy | |[公瑾讲解](https://www.youtube.com/watch?v=lBrb4fXPcMM)|
91+
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/#/description)| [Python [Yu]](./tree/Yu/563.py) | _O(N)_| _O(1)_ | Easy | |[公瑾讲解](https://youtu.be/47FQVP4ynk0)|

tree/Yu/563.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Author: Yu Zhou
5+
6+
# ****************
7+
# Descrption:
8+
# 563. Binary Tree Tilt
9+
# Given a binary tree, return the tilt of the whole tree.
10+
# 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.
11+
# The tilt of the whole tree is defined as the sum of all nodes' tilt.
12+
# ****************
13+
14+
# 思路:
15+
# 每层处理的事情就是增加Global Variable的变量
16+
# 然后返回左右两边以及本身的Sum
17+
18+
# ****************
19+
# Final Solution *
20+
# ****************
21+
class Solution(object):
22+
def findTilt(self, root):
23+
"""
24+
:type root: TreeNode
25+
:rtype: int
26+
"""
27+
self.res = 0
28+
def helper(root):
29+
if not root:
30+
return 0
31+
left = helper(root.left)
32+
right = helper(root.right)
33+
self.res += abs(left - right)
34+
return root.val + left + right
35+
helper(root)
36+
return self.res

0 commit comments

Comments
 (0)