Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. 操作给定的二叉树,将其变换为源二叉树的镜像。
  2. 二叉树的镜像定义:源二叉树
  3. 8
  4. / \
  5. 6 10
  6. / \ / \
  7. 5 7 9 11
  8. 镜像二叉树
  9. 8
  10. / \
  11. 10 6
  12. / \ / \
  13. 11 9 7 5
  14.  
  15. * solution1: Recursion
  16. ```python3
  17. # -*- coding:utf-8 -*-
  18. # class TreeNode:
  19. # def __init__(self, x):
  20. # self.val = x
  21. # self.left = None
  22. # self.right = None
  23. class Solution:
  24. # 返回镜像树的根节点
  25. def Mirror(self, root):
  26. # write code here
  27. if not root:
  28. return None
  29. temp = root.left
  30. root.left = root.right
  31. root.right = root.left
  32. self.Mirror(root.left)
  33. self.Mirror(root.right)
  34. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement