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 inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
递归解决方案:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root)
{
if(root ==NULL) return root;
TreeNode* node = invertTree(root->left);
root->left = invertTree(root->right);
root->right = node;
return root;
}
};
非递归解决方案:
TreeNode* invertTree(TreeNode* root)
{
if(root == NULL)return NULL;
vector<TreeNode*> stack;
stack.push_back(root);
while(!stack.empty())
{
TreeNode* node = stack.back();// or stack.top()
stack.pop_back();
swap(node->left,node->right);
if(node->left)stack.push_back(node->left);
if(node->right)stack.push_back(node->right);
}
return root;
}
python:
def invertTree(self, root):
if root:
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
return root Maybe make it four lines for better readability: def invertTree(self, root):
if root:
invert = self.invertTree
root.left, root.right = invert(root.right), invert(root.left)
return root -------------------------------------------------------------------------------- And an iterative version using my own stack: def invertTree(self, root):
stack = [root]
while stack:
node = stack.pop()
if node:
node.left, node.right = node.right, node.left
stack += node.left, node.right
return root
def invertTree(self, root):
if root is None:
return None
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
return root
python非递归解决方案:
DFS version: def invertTree(self, root):
if (root):
self.invertTree(root.left)
self.invertTree(root.right)
root.left, root.right = root.right, root.left
return root BFS version: def bfs_invertTree(self, root):
queue = collections.deque()
if (root):
queue.append(root) while(queue):
node = queue.popleft()
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 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- 226. Invert Binary Tree 翻转二叉树
[抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- [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 ...
- 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 ...
- LeetCode 226 Invert Binary Tree(转换二叉树)
翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNod ...
- Leetcode 226 Invert Binary Tree 二叉树
交换左右叶子节点 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- 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 ...
随机推荐
- Python中strip()、lstrip()、rstrip()用法详解
Python中有三个去除头尾字符.空白符的函数,它们依次为: strip: 用来去除头尾字符.空白符(包括\n.\r.\t.' ',即:换行.回车.制表符.空格)lstrip:用来去除开头字符.空白符 ...
- Python List insert()方法详解
1.功能insert()函数用于将指定对象插入列表的指定位置. 2.语法list.insert(index, obj) 3.参数index: 对象obj需要插入的索引位置.obj: 插入列表中的对象. ...
- MySql配置文件模板
写在开篇: 这个mysql得配置文件my.cnf,是我现在环境里常用得,包含基础配置及一些优化,本来一直在我得有道笔记里记录着,之前一直没有写博客的习惯,最近刚开始注册博客,就将这些东西 ...
- js数组排序,支持正反排序以及多维度排序
工作中遇到js数组排序问题,数组中存储的都是对象,于是就百度了下,利用别人的代码进行修改,最终完成可以倒序.反序,可以进行多维度排序的功能源码如下: /** * js数组排序 支持数字和字符串 * @ ...
- Docker常见仓库MySQL
MySQL 基本信息 MySQL 是开源的关系数据库实现. 该仓库提供了 MySQL 各个版本的镜像,包括 5.6 系列.5.7 系列等. 使用方法 默认会在 3306 端口启动数据库. $ sudo ...
- 实验与作业(Python)-文件操作
1.CSV文件的处理 下载-身份证号文件 导入: 读入"身份证号.txt",然后打印出来.注意:是否多打了一行,为什么? 读入"身份证号.txt",然后存储到& ...
- 衣带渐宽终不悔,为伊消得人憔悴--DbHelper增强版
核心理念 如何使用 测试实例 数据库内详细数据信息 测试代码 数据库连接池测试 测试集 延伸 相关下载链接 前几日,写了一篇关于一个 轻量级数据持久化的框架的博客(点击浏览: http://blog. ...
- SpringBatch前言
批处理应用程序就是对程序进行批量处理. 特点:批量应用程序处理大量数据而无需人类干预. 用途:可以选择使用批处理程序来计算每月的财务报表.计算统计和索引文件的数据等等. 应用:当银行系统中有很多业务都 ...
- leetcode 之 Single Number II
问题来源:Single Number II 问题描述:给定一个整数数组,除了一个整数出现一次之外,其余的每一个整数均出现三次,请找出这个出现一次的整数. 大家可能很熟悉另一个题目(Single Num ...
- Swift:一个简单的货币转换器App在iOS10中的分析和完善
这本不算是一个完整的货币转换App,只不过是一个小巧的学习性质的程序.该App覆盖了如下几个知识点: 多国语言的支持 通过网络Api接口读取数据 最后我们来修复一个原来代码中的一个小错误作为完美的收尾 ...