操作给定的二叉树,将其变换为源二叉树的镜像。
数据范围:二叉树的节点数
, 二叉树每个节点的值
要求: 空间复杂度
。本题也有原地操作,即空间复杂度
的解法,时间复杂度
比如:
源二叉树
镜像二叉树
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return TreeNode类 # class Solution: def Mirror(self , pRoot: TreeNode) -> TreeNode: # write code here if not pRoot: return pRoot return self.jiaohuan(pRoot) def jiaohuan(self,root): root.left,root.right= root.right,root.left if root.left: self.jiaohuan(root.left) if root.right: self.jiaohuan(root.right) return root
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return TreeNode类 # class Solution: def Mirror(self , root: TreeNode) -> TreeNode: # write code here ## 这里的镜像就相当于反转二叉树 if not root: return root root.left, root.right = root.right, root.left ## 反转左右 if root.left: self.Mirror(root.left) ## 进行递归 if root.right: self.Mirror(root.right) return root def Mirror(self , root: TreeNode) -> TreeNode: ### 用stack来完成反转(前序遍历的迭代法) ''' 1. 判断空root 2. 定义需要的数据结构,这里使用数组来作为栈stack 3. 将树结构压入定义的数据结构中 (将root压入append到stack中) 4. 弹出当前节点cur,然后对cur的相关信息进行操作 4.1 层序遍历:res.append(cur.val) 4.2 翻转(镜像):cur.left, cur.right = cur.right, cur.left 等 5. 判断 cur.left 和 cur.right 是否为空, 是空:结束了本次循环 否:继续进行入栈操作append() 和出栈操作pop() 6. 根据需要的数据结构,返回对应的情况 (有的需要列表,有的需要返回root) ''' if not root: return root stack = [] stack.append(root) while stack: cur = stack.pop() ## 书写方式和层序遍历差不多, cur.left, cur.right = cur.right, cur.left if cur.left: stack.append(cur.left) if cur.right: stack.append(cur.right) return root def Mirror(self , root: TreeNode) -> TreeNode: ## 使用层序遍历的方法完成反转 if not root: return root from collections import deque que = deque([root]) while que: size = len(que) res = [] for _ in range(size): cur = que.popleft() cur.left, cur.right = cur.right, cur.left if cur.left: que.append(cur.left) if cur.right: que.append(cur.right) return root def Mirror(self , root: TreeNode) -> TreeNode: ## 层序遍历 --》 数组 --》二叉树 ## 这个无法完全实现,但是通过数组生成二叉树的方法值得记住 results = [] if not root: return root from collections import deque que = deque([root]) while que: size = len(que) res = [] for _ in range(size): cur = que.popleft() res.append(cur.val) if cur.left: que.append(cur.left) if cur.right: que.append(cur.right) results.append(res) results_reverse = [] for alist in results: alist.reverse() results_reverse += alist ## print(results_reverse) return self.generage_TreeNode(results_reverse) def generage_TreeNode(self, nums): ### 无法得到 空置反转的情况 !!! def level(index): if index >= len(nums)&nbs***bsp;nums[index] is None: return None root = TreeNode(nums[index]) root.left = level(2*index+1) root.right = level(2*index+2) return root return level(0)
class Solution: def Mirror(self , pRoot: TreeNode) -> TreeNode: # write code here if not pRoot: return pRoot temp = pRoot.left pRoot.left = pRoot.right pRoot.right = temp pRoot.left = self.Mirror(pRoot.left) pRoot.right = self.Mirror(pRoot.right) return pRoot
class Solution: def Mirror(self , pRoot: TreeNode) -> TreeNode: # write code here if not pRoot: return None pRoot.left, pRoot.right = pRoot.right, pRoot.left self.Mirror(pRoot.left) self.Mirror(pRoot.right) return pRoot
class Solution: def Mirror(self , pRoot: TreeNode) -> TreeNode: if pRoot is None: return None pRoot.left, pRoot.right = pRoot.right, pRoot.left self.Mirror(pRoot.left) self.Mirror(pRoot.right) return pRoot
class Solution: def Mirror(self , pRoot: TreeNode) -> TreeNode: # write code here if not pRoot: return pRoot if pRoot.left and pRoot.right: pRoot.left,pRoot.right=pRoot.right,pRoot.left self.Mirror(pRoot.left) self.Mirror(pRoot.right) elif not pRoot.left: pRoot.left,pRoot.right=pRoot.right,pRoot.left self.Mirror(pRoot.left) elif not pRoot.right: pRoot.left,pRoot.right=pRoot.right,pRoot.left self.Mirror(pRoot.right) return pRoot
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return TreeNode类 # class Solution: def Mirror(self , pRoot: TreeNode) -> TreeNode: # write code here if not pRoot:return tmp=self.Mirror(pRoot.left) pRoot.left=self.Mirror(pRoot.right) pRoot.right=tmp return pRoot
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return TreeNode类 # class Solution: def Mirror(self , pRoot ): # write code here if pRoot!=None: temp=pRoot.left pRoot.left=pRoot.right pRoot.right=temp self.Mirror(pRoot.left) self.Mirror(pRoot.right) return pRoot