topic:
Thought:
Reference ylb 大佬的answer,
这道题andHiccup1and2The difference is,这道题是一棵Binary tree,And you can’t rob the neighboring node。
So his youngest child problem is:Robbing the current node,You can’t rob your left and right children。
If you steal root node,那么不能偷取其左右子node,Result root.val+lb +rb
;
If not steal root node,那么可以偷取其左右子node,Result max(la, lb)+max(ra ,rb)
。
Code:
1 | class Solution: |