来源:力扣 (LeetCode)
链接:https://leetcode-cn.com/problems/invert-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
一、题目描述
翻转一棵二叉树。
示例:
输入:
|
1 2 3 4 5 |
4 / \ 2 7 / \ / \ 1 3 6 9 |
输出:
|
1 2 3 4 5 |
4 / \ 7 2 / \ / \ 9 6 3 1 |
二、题解
简单题,有两种思路:递归和队列。
2.1 递归
每遍历到一个节点,调整左右子树的值。然后分别递归遍历左右子树。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
TreeNode *invertTree(TreeNode *root) { TreeNode *p; if (root == nullptr) { return nullptr; } // 调整左右子树 p = root->left; root->left = root->right; root->right = p; // 遍历左右子树 invertTree(root->left); invertTree(root->right); return root; } |
2.2 使用队列
把根节点放到队列中,然后依次访问队列中的节点,把每个节点的左右子树位置对换,然后把左右子节点放到栈中继续遍历。
|
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 |
TreeNode *invertTree(TreeNode *root) { queue<TreeNode *> q; TreeNode *node, *tmp; if (root == nullptr) { return nullptr; } // 根节点入队 q.push(root); while (!q.empty()) { // 取队首元素 node = q.front(); q.pop(); // 调换左右子树 tmp = node->left; node->left = node->right; node->right = tmp; // 左子树入队 if (node->left) { q.push(node->left); } // 右子树入队 if (node->right) { q.push(node->right); } } return root; } |
三、备注
这个问题是受到 Max Howell 的原问题启发的 :
谷歌:我们 90%的工程师使用您编写的软件 (Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。
其中的一个评论给也很有意思:
如果我是你,我会把白板从下到上翻转。然后告诉他:那,这就是我翻转的二叉树。
![[leetcode]226-翻转二叉树](https://www.dyxmq.cn/wp-content/uploads/2020/02/cff55-image8d13f59af4ceb4d0.png)


![【深搜入门题】[leetcode]46-全排列](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/efdc7-image64636fa9d8634cc4.png&w=280&h=210&a=&zc=1)


![[leetcode]199-二叉树的右视图](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/f24a8-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]72-编辑距离](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/88c60-imageff84a6c5047db6ed.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]460-LFU缓存](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/5b3f2-image.png&w=280&h=210&a=&zc=1)
![[leetcode]125-验证回文串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/5bfa5-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[程序员面试宝典]17.16-按摩师](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/9ecc9-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]876-链表的中间节点](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/64d0d-imagece778ab033c9d90d.png&w=280&h=210&a=&zc=1)
![【每日打卡】[剑指offer]面试题40-最小的k个数](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/c83c9-imagec745c425c670e18c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]-409最长回文子串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/e5589-image788d511d7c1e839c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]面试题1.6-字符串压缩](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/776bc-image.png&w=280&h=210&a=&zc=1)
评论