Leetcode 226. Invert Binary Tree
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1 使用递归的trival solution
class Solution(object):
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root:
return root.left, root.right = root.right, root.left
self.invertTree(root.right)
self.invertTree(root.left) return root
非递归的话,需要一个queue辅助。
class Solution(object):
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root:
return queue = [root] while queue:
node = queue.pop(0)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
node.left, node.right = node.right, node.left return root
Leetcode 226. Invert Binary Tree的更多相关文章
- LeetCode 226. 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 was ...
- Java for LeetCode 226 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 ...
- (easy)LeetCode 226.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 was ...
- Java [Leetcode 226]Invert Binary Tree
题目描述: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 解题思路: 我只想说递归大法好. ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- leetcode 226 Invert Binary Tree 翻转二叉树
大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...
- Leetcode 226. Invert Binary Tree(easy)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
- LeetCode (226):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 was ...
随机推荐
- python设计模式1:导言
<设计模式>一书总结了23个模式,依据各自的目的又被分为创建型模式(creational pattern).结构型模式(structural pattern)和行为型模式(behavior ...
- Hash链表
<?php /* +------------------------------------------------------------------------------ | dateti ...
- Debian 8.2 下安装MySQL5.7.9 Generic Binaries
安装过程参考了Installing MySQL on Unix/Linux Using Generic Binaries 首先检查是否安装libaio shell> apt-cache sear ...
- Nginx1.9.0的安装
下载文件 http://nginx.org/en/download.html 下载 nginx-1.9.3.tar.gz 安装Nginx 安装 一.安装nginx时必须先安装相应的编译工具 yum - ...
- (原创)mybatis学习四,利用mybatis自动创建代码
在使用mybatis的过程中,我们可以直接利用MyBatis生成器自动生成实体类.DAO接口和Mapping映射文件,然后copy到工程中即可 需要的jar包如下 下载路径如下:下载jar包 其中的g ...
- [转]C#压缩打包文件
/// <summary> /// 压缩和解压文件 /// </summary> public class ZipClass { /// <summary> /// ...
- python强大的区间处理库interval用法介绍
原文发表在我的博客主页,转载请注明出处 前言 这个库是在阅读别人的源码的时候看到的,觉得十分好用,然而在网上找到的相关资料甚少,所以阅读了源码来做一个简单的用法总结.在网络的路由表中,经常会通过掩码来 ...
- 腾讯 or 华为 =》 求职者的困惑
本文目的: 希望有老司机指点迷津 个人背景: 本人软件工程专业,硕士研究生,2017年7月毕业,个人喜欢Java开发,希望有机会从事Java分布式应用开发 故事背景一: 本人2016年4月份参加了腾讯 ...
- linux命令行安装使用KVM
一.说明 本篇文章介绍的是基于centos环境来安装的,ip地址192.168.4.233 二.检查CPU是否支持虚拟技术 egrep 'vmx|svm' /proc/cpuinfo 如果有输出内容表 ...
- 外网不能访问部署在虚机的NodeJs网站(80端口)
外网能访问部署在虚机的NodeJs网站需注意如下: 在管理门户上配置端点(Http 80->80) 在虚机中的防火墙入站规则中增加应用程序Node.exe的允许规则 启动NodeJs的侦听进程时 ...