题目地址:Invert Binary Tree - LeetCode


Invert a binary tree.

Example:

Input:

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

Output:

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

Trivia:
This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.


这道题目是经典的反转二叉树,用递归来做非常快。
Python解法如下:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def invertTree(self, root: TreeNode) -> TreeNode:if root==None:returnroot.left,root.right=root.right,root.leftself.invertTree(root.left)self.invertTree(root.right)return root

迭代的做法麻烦很多,迭代有BFS和DFS共2种做法,DFS解法如下。
Python解法如下:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def invertTree(self, root: TreeNode) -> TreeNode:stack = [root]while stack:node = stack.pop()if node:node.left, node.right = node.right, node.leftstack.extend([node.right, node.left])return root

C++解法如下:

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution
{public:TreeNode *invertTree(TreeNode *root){if (root == nullptr){return root;}TreeNode *temp = root->left;root->left = root->right;root->right = temp;invertTree(root->left);invertTree(root->right);return root;}
};

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

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

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

  2. Leetcode 226: Invert Binary Tree

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

  3. 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 整理 ...

  4. 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 ...

  5. 226. Invert Binary Tree 翻转二叉树

    翻转一棵二叉树. 示例: 输入: 4/ \2 7/ \ / \ 1 3 6 9 输出: 4/ \7 2/ \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell 的 原问题 启发的 ...

  6. 226. Invert Binary Tree 1

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

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

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

  8. 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 ...

  9. 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 思路分析: 题意是将二叉树所有左右子数 ...

最新文章

  1. docker保护python源码_Tensorflow在Docker中运行和源码编译
  2. PDF审计工具peepdf
  3. 这个开源项目...看了就停不下来啊!
  4. 解决Failed to load class org.slf4j.impl.StaticLoggerBinder
  5. 《Android程序设计》一第一部分 工具和基础
  6. 游戏数仓分析(二)SpringBoot项目对数据进行可视化展示
  7. MyEclipse 启动之 java.lang.RuntimeException: No application id has been
  8. AjaxPro 未定义错误
  9. 重磅:向996开炮!携程带头居家办公。
  10. RFC 791翻译(IP的主要内容)
  11. 我的第二个切换图片高亮显示,给力
  12. Scarlett~スカーレット 有感,新的价值观
  13. android exif 相册,android 读取图片 EXIF信息
  14. Go第八篇之包的使用
  15. python使用pika库调用rabbitmq的交换机模式
  16. 网卡能通过usb接口与计算机连接吗,usb网卡怎么使用 usb网卡使用方法【图文】...
  17. 利用MATLAB视频函数工具箱的…
  18. nginx 配置https 443端口配置
  19. 空气炸锅如何挑选研究
  20. python3中使用pip3错误syn_python-pip3错误-'_NamespacePath'对象没有属性'sort'

热门文章

  1. Drug Discovery Today | 频繁命中化合物机制探究:PAINS规则的局限性
  2. OpenCV | ORB特征检测与描述
  3. 第二十二课.DeepGraphLibrary(三)
  4. Java中Math类常用函数总结
  5. mSystems:南土所梁玉婷组-土壤pH过滤稻田土壤耐铝微生物的关联模式
  6. Microbiome:地球上有多大比例的原核生物已经被测序了基因组?
  7. Docker的基本使用-Ubuntu18.04
  8. Python使用matplotlib可视化散点图、并在可视化图像的底部和右边添加边缘箱图(Marginal Boxplot)
  9. matplotlib可视化基本散点图、在图像指定区域绘制方框(Draw Rectangle)
  10. R语言ggplot2可视化柱状图(条形图)可视化中保留没有用到的因子水平(占位保留)