Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def countPairs(self, root1, root2, x):
- cur1,cur2=root1,root2
- st1,st2=deque(),deque()
- count=0
- while True:
- while cur1:
- st1.append(cur1)
- cur1=cur1.left
- while cur2:
- st2.append(cur2)
- cur2=cur2.right
- if not st1 or not st2:
- break
- top1,top2=st1[-1],st2[-1]
- sum=top1.data+top2.data
- if sum<x:
- st1.pop()
- cur1=top1.right
- elif sum>x:
- st2.pop()
- cur2=top2.left
- else:
- count+=1
- st1.pop()
- st2.pop()
- cur1,cur2=top1.right,top2.left
- return count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement