- Leetcode 563. Binary Tree Tilt
- Level: easy
- Link
My Approach
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findTilt(self, root: TreeNode) -> int:
total_tilt = 0
def dfs(node):
nonlocal total_tilt
if not node:
return 0
left_sum = dfs(node.left)
right_sum = dfs(node.right)
tilt = abs(left_sum - right_sum)
total_tilt += tilt
return left_sum + right_sum + node.val
dfs(root)
return total_tilt
'개발공부 > algorithm' 카테고리의 다른 글
[Leetcode][python] 1137. N-th Tribonacci Number - DP (0) | 2021.05.16 |
---|---|
[Leetcode][python] 938: Range sum BST - BFS (0) | 2021.05.16 |
[Leetcode][python] 107: Binary Tree Level order Traversal II (0) | 2021.05.15 |
[프로그래머스][python] 프린터 - Stack/queue (0) | 2021.05.08 |
[Leetcode][python] 482: License key formatting - String (0) | 2021.05.01 |