翻转二叉树

翻转一棵二叉树。左右子树交换。

Example

样例 1:

输入: {1,3,#}
输出: {1,#,3}
解释:
1 1
/ => \
3 3

样例 2:

输入: {1,2,3,#,#,4}
输出: {1,3,2,#,4}
解释: 1 1
/ \ / \
2 3 => 3 2
/ \
4 4

Challenge

递归固然可行,能否写个非递归的?

代码:

"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
""" class Solution:
"""
@param root: a TreeNode, the root of the binary tree
@return: nothing
"""
def invertBinaryTree(self, root):
# write your code here
if not root:
return
self.invertBinaryTree(root.left)
self.invertBinaryTree(root.right)
root.left, root.right = root.right, root.left
  

当然,使用先序遍历也可以

算法 翻转二叉树 dfs的更多相关文章

  1. LeetCode:翻转二叉树【226】

    LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...

  2. 聊聊算法——BFS和DFS

    如果面试字节跳动和腾讯,上来就是先撕算法,阿里就是会突然给你电话,而且不太在意是周末还是深夜, 别问我怎么知道的,想确认的可以亲自去试试.说到算法,直接力扣hard三百题也是可以的,但似乎会比较伤脑, ...

  3. LeetCode226 翻转二叉树

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

  4. [LeetCode] Invert Binary Tree 翻转二叉树

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...

  5. lintcode :Invert Binary Tree 翻转二叉树

    题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...

  6. [Swift]LeetCode226. 翻转二叉树 | Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...

  7. leetcode 翻转二叉树

    翻转二叉树的步骤: 1.翻转根节点的左子树(递归调用当前函数) 2.翻转根节点的右子树(递归调用当前函数) 3.交换根节点的左子节点与右子节点 class Solution{ public: void ...

  8. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  9. 【leetcode 简单】 第六十四题 翻转二叉树

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

随机推荐

  1. Oracle_本地计算机上的OracleOraDb11g_home1TNSListener 服务启动后停止

    这个IP地址要写对,写成本机IP4的地址

  2. Linux 下配置 iSCSI 客户端

    安装客户端软件 Redhat/Centos: yum install -y iscsi-initiator-utils Debian/Ubuntu: apt-get install open-iscs ...

  3. Shell脚本之六 数学计算

    前面一节Shell篇之五 基本运算符介绍了常见的 Shell 算术运算符,这节介绍 Shell 的数学计算.Shell 和其它编程语言不同,Shell 不能直接进行算数运算,必须使用数学计算命令. 下 ...

  4. Mysql 学习参考

    [1]Mysql 基础知识 (1)<Mysql 官网> (2)<菜鸟教程之Mysql数据库教程> (3)<C语言中文网之Mysql数据库栏> (4)<W3Sc ...

  5. MyBatis系列(二) MyBatis接口绑定与多参数传递

    前言 通过上一篇博文的,已经可以做到通过MyBatis连接数据库,现在再来介绍一种方法通过接口绑定SQL语句. 不使用接口绑定的方式 不使用接口绑定的方式,是通过调用SqlSession中的selec ...

  6. C# Mysql数据库备份、还原(MVC)

    一.准备工作 1.电脑上要安装上mysql,并且已经配置好了环境变量. 二.公共代码 1.配置文件(该节点只是为备份.还原使用,数据库连接字符串有另外的节点) <connectionString ...

  7. Kubeadm 1.9 HA 高可用集群本地离线镜像部署【已验证】

    k8s介绍 k8s 发展速度很快,目前很多大的公司容器集群都基于该项目,如京东,腾讯,滴滴,瓜子二手车,易宝支付,北森等等. kubernetes1.9版本发布2017年12月15日,每三个月一个迭代 ...

  8. YAML语言简明教程

    编程免不了要写配置文件,如果你还在用xml/ini/json,就有点过时了,怎么写配置也是一门学问. YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便. 本文介绍 YAM ...

  9. Spring Boot 企业级应用开发实战 刘伟东-2018年3月第一版

    Spring会自动搜索某些路径下的Java类 并将这些类注册微Bean实例,这样就省去了所有Bean都配置在XML的麻烦 Spring会适当地将显示指定路径下的的类全部注册微Spring Bean . ...

  10. 7.JavaScript-Promise的并行和串行

    Promise 并行 Promise.all是所有的Promise执行完毕后(reject|resolve)返回一个Promise对象. 最近在开发一个项目中,需要等接口拿到全部数据后刷新页面,取消l ...