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否是空.有的 ...
随机推荐
- 福大软工 · 第十二次作业 - Beta答辩总结之拖鞋旅游队
目录 前言 项目的链接与宣传 项目总结 原计划 达成情况 原因分析 [ Beta 冲刺博客链接汇总] [燃尽图] Beta版本展示 使用说明 视频展示 图片展示 答辩总结 [团队中个人的贡献比例] [ ...
- L335 Nasa’s Twin Study Reveals Effects of Time Spent in Space on the Human Body
What exactly happens to a human body in space? Despite decades of astronauts going on space missions ...
- MySQL:视图
视图 一.视图的概述 1. 含义:是从数据库中一个或多个表中导出的虚拟表2. 作用:①简单化 ②安全性 ③逻辑数据独立性3. 注意:一个表可以由多个视图: 二.视图的创建 1. 总的语法形式 CREA ...
- 二,编程语言类别,和python变量基础
编程语言类别 机器语言:由二进制组成,直接控制操作硬件,执行效率高,开发效率低. 汇编语言:用英文代替二进制,直接操作控制硬件,执行效率高,开发效率低. 高级语言: 编译型,如C语言,类似谷歌翻译,先 ...
- 网页全屏,modal 弹框无法显示的问题
问题描述页面主体部分全屏后,页面中的所有弹窗不能显示,退出全屏后,弹窗正常.解决方法校园项目中,所有用到的弹窗为iview的弹窗组件,该弹窗组件会生成在body中,和项目主体app为平行关系,项目主体 ...
- 剑指Offer 31. 整数中1出现的次数(从1到n整数中1出现的次数) (其他)
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- js实现table表格相同内容按需合并
uniteTdCells(tableId) { var table = document.getElementById(tableId); for (let i = 0; i < table.r ...
- DAX创建带有过滤器的超链接
在这篇文章中,我们将创建一个DAX公式,根据报表中的过滤器生成超链接. 该度量包含2个部分,第一部分是使用DAX生成目标报告的正确URL,第二部分是将过滤器传递给该报告. 浏览器支持的MAX网址长度定 ...
- Post Order traverse binary tree using non-recursive way
Process analysis Stack = 5, Push 3, Stack = 5, 3. Pre = 5 Current = 3, Pre = 5, Push 2 to the st ...
- ieee trans pami latex模板
https://www.computer.org/cms/Computer.org/transactions/templates/ https://www.computer.org/web/tpami ...