• 문제 링크
Binary Tree Tilt - LeetCode
Can you solve this real interview question? 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 subtr
leetcode.com
• 풀이 코드
public class Solution {
int sum = 0;
public int findTilt(TreeNode root) {
sum(root);
return sum;
}
private int sum(TreeNode node) {
if (node == null) return 0;
int l = sum(node.left), r = sum(node.right);
sum += Math.abs(l - r);
return l + r + node.val;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 572. Subtree of Another Tree - Java (0) | 2026.09.04 |
|---|---|
| [LeetCode] 566. Reshape the Matrix - Java (0) | 2026.09.03 |
| [LeetCode] 561. Array Partition - Java (0) | 2026.09.01 |
| [LeetCode] 559. Maximum Depth of N-ary Tree - Java (0) | 2026.08.31 |
| [LeetCode] 557. Reverse Words in a String III - Java (0) | 2026.08.30 |
댓글