Skip to content

Commit b5c1ae0

Browse files
committed
Solution to 1022
1 parent 7604db4 commit b5c1ae0

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/unresolved/1022.sum-of-root-to-leaf-binary-numbers.jl renamed to src/problems/1022.sum-of-root-to-leaf-binary-numbers.jl

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1022. Sum of Root To Leaf Binary Numbers
33
# id: problem1022
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Indigo
5+
# date: 2022-03-19
66
# difficulty: Easy
77
# categories: Tree
88
# link: <https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/description/>
@@ -69,5 +69,12 @@
6969
## @lc code=start
7070
using LeetCode
7171

72-
## add your code here:
72+
_sum_root_to_leaf(::Nothing) = (0, 0)
73+
function _sum_root_to_leaf(root::TreeNode{Int})
74+
lft, rgt = _sum_root_to_leaf(root.left), _sum_root_to_leaf(root.right)
75+
tms = max(1, lft[2] + rgt[2])
76+
return (lft[1] + rgt[1]) + tms * root.val, tms * 2
77+
end
78+
79+
sum_root_to_leaf(root::TreeNode{Int}) = _sum_root_to_leaf(root)[1]
7380
## @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@testset "1022.sum-of-root-to-leaf-binary-numbers.jl" begin
2+
@test sum_root_to_leaf(TreeNode{Int}([1,0,1,0,1,0,1])) == 22
3+
@test sum_root_to_leaf(TreeNode(1)) == 1
4+
@test sum_root_to_leaf(TreeNode(0)) == 0
5+
@test sum_root_to_leaf(TreeNode{Int}([1, 1])) == 3
6+
end

0 commit comments

Comments
 (0)