class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: return root def helper(node): if node: if node.left or node.right: node.left,node.right=node.right,node.left helper(node.left) helper(node.right) helper(root) return root
执行用时 :48 ms, 在所有 python3 提交中击败了47.66%的用户
内存消耗 :13.9 MB, 在所有 python3 提交中击败了5.24%的用户
——2019.11.19
public TreeNode invertTree(TreeNode root) { if(root == null) return null; TreeNode temp = root.left; root.left = root.right; root.right = temp; invertTree(root.left); invertTree(root.right); return root; }
递归