翻转一棵二叉树。

示例:

输入:

     4/   \2     7/ \   / \
1   3 6   9

输出:

     4/   \7     2/ \   / \
9   6 3   1

备注:
这个问题是受到 Max Howell 的 原问题 启发的 :

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

递归

这题,比较简单,看代码就能看懂。

Code

 def invertTree(self, root: TreeNode) -> TreeNode:if root:self.invertTree(root.left)self.invertTree(root.right)root.right, root.left = root.left, root.rightreturn root

226. Invert Binary Tree 翻转二叉树相关推荐

  1. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  2. 226. Invert Binary Tree 1

    题目链接:Invert Binary Tree 思路: 如果需要反转一个二叉树,那么我们需要遍历整个树的所有节点. 如果想遍历所有的节点,我们可以用Depth First Search(DFS)或者B ...

  3. leetcode python3 简单题226. Invert Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十六题 (1)题目 英文: Invert a binary tree. 中文 ...

  4. LeetCode #226 - Invert Binary Tree - Easy

    Problem Invert a binary tree. Example 4/ \2 7/ \ / \ 1 3 6 9->4/ \7 2/ \ / \ 9 6 3 1 Algorithm 整理 ...

  5. [LeetCode][JavaScript]Invert Binary Tree 反转二叉树

    反转二叉树 其实我从没有想到前端面试会问到这个问题,题目来源于google的面试 Google: 90% of our engineers use the software you wrote (Ho ...

  6. Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树所有左右子数 ...

  7. Leetcode-标签为Tree 226. Invert Binary Tree

    原题 Invert a binary tree. 4/ \ 2 7/ \ / \ 1 3 6 9 to 4/ \ 7 2/ \ / \ 9 6 3 1 代码分析 反转二叉树 代码实现 public T ...

  8. Leetcode 226: Invert Binary Tree

    问题描述: Given the root of a binary tree, invert the tree, and return its root. 将二叉树镜像 思路: 发现子问题:将给定二叉树 ...

  9. leetcode 226. Invert Binary Tree

    Invert a binary tree. 4/ \2 7/ \ / \ 1 3 6 9 to 4/ \7 2/ \ / \ 9 6 3 1 解法1: 本质是输的先序遍历 # Definition f ...

最新文章

  1. idea 高效找出全部未被使用的代码
  2. IP地址与字符串、整型之间的转换
  3. 利用jquery getJSON 调用ashx实现ajax调用
  4. 世界顶级的程序员们告诉你:这些书都是你应该读的
  5. Android 集成高德地图——当前定位,添加图标,画路线,设置显示中心位置,比例,地图刷新位置监听,判断GPS开启,去打开GPS
  6. jQuery操作cookie
  7. java 高效文本查找替换_Java 查找、高亮PDF 文本
  8. 动手学深度学习(PyTorch实现)(二)--softmax与分类模型
  9. 长假终结踏上归途 网络电话延续团圆亲情
  10. 3升5升得4升——倒水问题的万能解法(扩展欧几里得算法)
  11. MATLAB信号的带宽估计及求带宽(3dB,90%带宽)
  12. stm32F407中arr与psc以及pwm之间的关系
  13. 物联网NB-IoT技术商用正全面铺开 竞争日趋激烈
  14. ES6 极简教程(ES6 Tutorial) 文 / 东海陈光剑
  15. 大天使黎明服务器维护,37大天使之剑3月5日部分区服维护公告
  16. WCF 项目应用连载[9] - 契约中的委托 事件参数处理
  17. mvc原理和mvc模式的优缺点
  18. Python编程与其他编程语言相比有何优势?
  19. mvn Mac Linux 版本,Mac 下 Maven 的命令行安装
  20. 软件光盘制作教程之—菜鸟篇

热门文章

  1. 洛谷——P1342 请柬
  2. MySQL下perror工具查看System Error Code信息
  3. python 压缩 解压
  4. hadoop 计数器
  5. 【转】PowerShell入门(五):Cmd命令与PowerShell命令的交互
  6. input border IE6 bug
  7. shell 学习笔记(四)
  8. XX管理包括哪些功能
  9. DotnetCharting
  10. 魅族用鸿蒙系统吗,魅族宣布接入鸿蒙是怎么回事?魅族手机可以刷鸿蒙系统吗?...