Skip to content

Commit 10bad32

Browse files
committed
Sync LeetCode submission Runtime - 1 ms (93.72%), Memory - 21.4 MB (25.69%)
1 parent 8c36701 commit 10bad32

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

0663-equal-tree-partition/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<p>Given the <code>root</code> of a binary tree, return <code>true</code><em> if you can partition the tree into two trees with equal sums of values after removing exactly one edge on the original tree</em>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/split1-tree.jpg" style="width: 500px; height: 204px;" />
6+
<pre>
7+
<strong>Input:</strong> root = [5,10,10,null,null,2,3]
8+
<strong>Output:</strong> true
9+
</pre>
10+
11+
<p><strong class="example">Example 2:</strong></p>
12+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/split2-tree.jpg" style="width: 277px; height: 302px;" />
13+
<pre>
14+
<strong>Input:</strong> root = [1,2,10,null,null,2,20]
15+
<strong>Output:</strong> false
16+
<strong>Explanation:</strong> You cannot split the tree into two trees with equal sums after removing exactly one edge on the tree.
17+
</pre>
18+
19+
<p>&nbsp;</p>
20+
<p><strong>Constraints:</strong></p>
21+
22+
<ul>
23+
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
24+
<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
25+
</ul>

0663-equal-tree-partition/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Approach: Depth First Search
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
# Definition for a binary tree node.
7+
# class TreeNode:
8+
# def __init__(self, val=0, left=None, right=None):
9+
# self.val = val
10+
# self.left = left
11+
# self.right = right
12+
13+
class Solution:
14+
def checkEqualTree(self, root: Optional[TreeNode]) -> bool:
15+
seen = []
16+
17+
def sum_(node):
18+
if not node:
19+
return 0
20+
seen.append(sum_(node.left) + sum_(node.right) + node.val)
21+
return seen[-1]
22+
23+
total = sum_(root)
24+
seen.pop()
25+
return total / 2.0 in seen
26+

0 commit comments

Comments
 (0)