solution.md 2.9 KB
Newer Older
ToTensor's avatar
ToTensor 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# 翻转二叉树

<p>翻转一棵二叉树。</p>

<p><strong>示例:</strong></p>

<p>输入:</p>

<pre>     4
   /   \
  2     7
 / \   / \
1   3 6   9</pre>

<p>输出:</p>

<pre>     4
   /   \
  7     2
 / \   / \
9   6 3   1</pre>

<p><strong>备注:</strong><br>
这个问题是受到 <a href="https://twitter.com/mxcl" target="_blank">Max Howell </a><a href="https://twitter.com/mxcl/status/608682016205344768" target="_blank">原问题</a> 启发的 :</p>

<blockquote>谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。</blockquote>


## template

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution(object):
    def invertTree(self, root):
        """
		:type root: TreeNode
		:rtype: TreeNode
		"""

        if not root:
            return None
        root.left, root.right = root.right, root.left

        self.invertTree(root.left)
        self.invertTree(root.right)

        return root
```

## 答案

```python
59 60 61 62 63 64
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

ToTensor's avatar
ToTensor 已提交
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
class Solution(object):
    def invertTree(self, root):
        """
		:type root: TreeNode
		:rtype: TreeNode
		"""

        if not root:
            return None
        root.left, root.right = root.right, root.left

        self.invertTree(root.left)
        self.invertTree(root.right)

        return root
ToTensor's avatar
ToTensor 已提交
81 82 83 84 85 86 87
```

## 选项

### A

```python
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution(object):
    def invertTree(self, root):
        """
		:type root: TreeNode
		:rtype: TreeNode
		"""

        if not root:
            return None
        self.invertTree(root.left)
        self.invertTree(root.right)
        root.left, root.right = root.right, root.left
ToTensor's avatar
ToTensor 已提交
107

108
        return root
ToTensor's avatar
ToTensor 已提交
109 110 111 112 113
```

### B

```python
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution(object):
    def invertTree(self, root):
        """
		:type root: TreeNode
		:rtype: TreeNode
		"""

        if not root:
            return None
        self.invertTree(root.left)
        root.left, root.right = root.right, root.left
        self.invertTree(root.right)
ToTensor's avatar
ToTensor 已提交
133

134
        return root
ToTensor's avatar
ToTensor 已提交
135 136 137 138 139
```

### C

```python
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution(object):
    def invertTree(self, root):
        """
		:type root: TreeNode
		:rtype: TreeNode
		"""

        if not root:
            return None
        self.invertTree(root.right)
        root.left, root.right = root.right, root.left
        self.invertTree(root.left)
ToTensor's avatar
ToTensor 已提交
159

160
        return root
ToTensor's avatar
ToTensor 已提交
161
```