leetcode226
/**
* 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的更多相关文章
- 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 ...
- [LeetCode226]Invert Binary Tree
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 反转二叉树,左右儿子值交换 代码: / ...
- [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 ...
- LeetCode226 InvertBinaryTree Java题解
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 解答: 遍历每个节点 直接交换他们的 ...
- 第27题:Leetcode226: Invert Binary Tree反转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 思路 如果根节点存在,就交换两个子树的根节点,用递归 ...
- LeetCode226 翻转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题 ...
- Python爬虫简单实现CSDN博客文章标题列表
Python爬虫简单实现CSDN博客文章标题列表 操作步骤: 分析接口,怎么获取数据? 模拟接口,尝试提取数据 封装接口函数,实现函数调用. 1.分析接口 打开Chrome浏览器,开启开发者工具(F1 ...
- 剑指 Offer 27. 二叉树的镜像
同LeetCode226翻转二叉树 1 class Solution { 2 public: 3 TreeNode* mirrorTree(TreeNode* root) { 4 if(root == ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
随机推荐
- get 乱码解决方案
1.针对Post方式提交的请求如果出现乱码,可以每次在request解析数据时设置编码格式: request.setCharacterEncoding("utf-8"); 也可以使 ...
- GOPATH
环境变量 GOPATH 的值可以是一个目录的路径,也可以包含多个目录路径,每个目录都代表 Go 语言的一个工作区(workspace).这些工作区用于放置 Go 语言的源码文件(source file ...
- $_SERVER['HTTP_REFER'] 和 session cookie 关系
对Session和Cookie的区分与理解 先说session 对SESSION的争论好象一直没有停止过,不过幺麽能理解SESSION的人应该占90以上.但还是讲讲,别嫌老~ 有一些人赞成用SESSI ...
- ASP.NET MVC 阻止通过Url直接访问服务器上的静态文件
某些情况下我们需要在服务器上保存一些静态文件,比如用户上传到服务器的文件,如果刚好这些文件的保存目录是应用程序目录下的一个子目录的话,别人就可以通过Url直接访问这个文件. 例如:在应用程序目录下的U ...
- 自动化测试-5.python基本语法
# encoding=utf-8 import sys import time # 我想从键盘输入信息 name ='' #赋值为空 print name #输出空 name=raw_input(&q ...
- 【软件安装与环境配置】TX2刷机过程
前言 使用TX2板子之前需要进行刷机,一般都是按照官网教程的步骤刷机,无奈买不起的宝宝只有TX2核心板,其他外设自己搭建,所以只能重新制作镜像,使用该镜像进行刷机. 系统需求 1.Host Platf ...
- Cortex-M3的一些概念
[工作模式] 线程模式(Thread mode):处理器复位或异常退出时为此模式.此模式下的代码可以是特权代码也可以是用户代码,通过CONTROL[0]控制.处理模式(Handler mode):出现 ...
- 洛谷P1219 :八皇后(DFS+回溯)
题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...
- java-Set集合、HashSet集合、LinkedHashSet集合和TreeSet集合
1.Set集合,无索引,不可以重复,无序(存取不一致) public class Demo { public static void main(String[] args) { //demo1(); ...
- RabbitMQ python模块pika生产者消费者轮询模型。
完整代码如下: 生产者,producer import pika connection = pika.BlockingConnection( pika.ConnectionParameters('lo ...