Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
mergeTrees
class TreeNode(object): def __init__(self,x): self.val = x self.left = None self.right = None class Solution(object): def mergeTrees(self,t1,t2): if t1 is not None and t2 is not None: t1.val += t2.val if t1.left is not None and t2.left is not None: self.mergeTrees(t1.left,t2.left) if t1.right is not None and t2.right is not None: self.mergeTrees(t1.right,t2.right) elif t1.right is None and t2.right is not None: t1.right = t2.right return t1 res = [] def tree(self,result): if result is not None: self.res.append(result.val) if result.left is not None: self.tree(result.left) if result.right is not None: self.tree(result.right) return self.res # Testing t1 = TreeNode(1) t1.left = TreeNode(3) t1.right = TreeNode(2) t1.left.left = TreeNode(5) t2 = TreeNode(2) t2.left = TreeNode(1) t2.left.right = TreeNode(4) t2.right = TreeNode(3) t2.right.right = TreeNode(7) soln = Solution() print soln.tree(soln.mergeTrees(t1,t2)) soln.res = [] print soln.tree(soln.mergeTrees(t2,t1))
run
|
edit
|
history
|
help
0
PyClassInit
Problem Name: single_digit
binary
Python learning
working with strings
gameboy
single digit
gameboy
Attempt to fork bomb server
fb_series