| 140 | self.maxes = -float('inf') |
| 141 | |
| 142 | def helper(root): |
| 143 | myValue = root.val |
| 144 | if not root.left and not root.right: |
| 145 | self.maxes = myValue if myValue > self.maxes else self.maxes |
| 146 | return myValue |
| 147 | |
| 148 | valueLeft, valueRight = -float('inf'), -float('inf') |
| 149 | |
| 150 | if root.left: |
| 151 | valueLeft = helper(root.left) |
| 152 | |
| 153 | if root.right: |
| 154 | valueRight = helper(root.right) |
| 155 | |
| 156 | # judge left to right is max or not. |
| 157 | |
| 158 | self.maxes = max([myValue + valueLeft, myValue + valueRight, myValue + valueLeft + valueRight, myValue, self.maxes]) |
| 159 | |
| 160 | # return to parent |
| 161 | return max(myValue + max(valueLeft, valueRight), myValue) |
| 162 | |
| 163 | helper(root) |
| 164 | return self.maxes |