/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode InvertTree(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) return root; TreeNode left = root.left;
root.left = InvertTree(root.right);
root.right = InvertTree(left);
return root;
}
}

https://leetcode.com/problems/invert-binary-tree/#/description

补充一个使用层次遍历处理的方案,java实现

 class Solution {
public TreeNode invertTree(TreeNode root) {
List<TreeNode> nodes = new LinkedList<>();
if (root != null) {
nodes.add(root);
}
while (!nodes.isEmpty()) {
int numNodes = nodes.size();
while (numNodes > 0) {
TreeNode node = nodes.remove(0);
// push children to the next layer by appending to the end of the list
if (node.left != null) {
nodes.add(node.left);
}
if (node.right != null) {
nodes.add(node.right);
}
// flip
TreeNode left = node.left;
node.left = node.right;
node.right = left;
numNodes--;
}
}
return root;
}
}

补充一个python的实现:

 class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
if root == None:
return None
left = root.left
root.left = self.invertTree(root.right)
root.right = self.invertTree(left)
return root

leetcode226的更多相关文章

  1. Leetcode226:Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 /** * Definition for a ...

  2. [LeetCode226]Invert Binary Tree

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 反转二叉树,左右儿子值交换 代码: / ...

  3. [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 ...

  4. LeetCode226 InvertBinaryTree Java题解

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 解答: 遍历每个节点  直接交换他们的 ...

  5. 第27题:Leetcode226: Invert Binary Tree反转二叉树

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1  思路 如果根节点存在,就交换两个子树的根节点,用递归 ...

  6. LeetCode226 翻转二叉树

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

  7. Python爬虫简单实现CSDN博客文章标题列表

    Python爬虫简单实现CSDN博客文章标题列表 操作步骤: 分析接口,怎么获取数据? 模拟接口,尝试提取数据 封装接口函数,实现函数调用. 1.分析接口 打开Chrome浏览器,开启开发者工具(F1 ...

  8. 剑指 Offer 27. 二叉树的镜像

    同LeetCode226翻转二叉树 1 class Solution { 2 public: 3 TreeNode* mirrorTree(TreeNode* root) { 4 if(root == ...

  9. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

随机推荐

  1. MySQL:函数

    函数 一.数学函数 1.绝对值函数ABS(x): x为插入的数据,返回绝对值 2.返回圆周率函数PI(): 无需插入数据,返回圆周率的值,默认为小数点后6位 3.平方根函数SQRT(x): 返回非负数 ...

  2. APP打包提交审核的步骤

  3. redis 分布式锁实现

    我们实现的分布式锁,使用redis提供的SET NX功能,由于redis server的单线程模型,保证了天然并发安全. https://stackoverflow.com/questions/116 ...

  4. 全栈爬取-Scrapy框架(CrawlSpider)

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  5. CoAP、MQTT、RESTful协议区别

    /********************************************************************** * CoAP.MQTT.RESTful协议区别 * 说明 ...

  6. Mac + PyCharm 安装 Opencv3 + python2.7

    本文地址:http://www.cnblogs.com/QingHuan/p/7354074.html 转载请注明本文地址,方便读者查看本文更新,谢谢! 今天要在Mac上安装OpenCV,过程非常曲折 ...

  7. springboot笔记1(转载于puresmile)

    构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

  8. s21day07 python笔记

    s21day07 python笔记 一.昨日内容回顾及补充 回顾 补充 将前面所提到的功能,统一改称为方法 二.深浅拷贝 基本格式 v1 = [1,2,3] import copy v2 = copy ...

  9. 20155208徐子涵 Exp4 恶意代码分析

    20155208徐子涵 Exp4 恶意代码分析 实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使 ...

  10. C++学习(三十九)(C语言部分)之 游戏项目(2048游戏)

    /***************************项目 2048**********************c语言编写 图形库制作时间:2019.04.03 准备工具: vs2013 图形库 i ...