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 ...
随机推荐
- Git/GitHub SSH配置
生成 SSH 公钥 如前所述,许多 Git 服务器都使用 SSH 公钥进行认证. 为了向 Git 服务器提供 SSH 公钥,如果某系统用户尚未拥有密钥,必须事先为其生成一份. 这个过程在所有操作系统上 ...
- vim 基本命令入门
简介 vim是Linux 系统下类似于Windows的记事本的编辑器. vim 中经常使用的三种模式 一般模式:浏览文件内容. 插入模式:编辑文件内容. 底行模式:进行保存编辑内容,退出等操作. 基本 ...
- 转:window与linux互相拷贝文件
window与linux互相拷贝文件 借助 PSCP 命令可以实现文件的互拷: 1.下载pscp.exe 文件 (我的资源文件中有) 2.如果想在所有目录可以执行,请更改环境变量. w ...
- Hibernate异常之Integer转float(自动类型转换错误)
错误代码: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float at org.hiber ...
- 安卓Tv开发(一)移动智能电视之焦点控制(触控事件)
前言:移动智能设备的发展,推动了安卓另一个领域,包括智能电视和智能家居,以及可穿戴设备的大量使用,但是这些设备上的开发并不是和传统手机开发一样,特别是焦点控制和用户操作体验风格上有很大的区别,本系列博 ...
- 项目分享:通过使用SSH框架的公司-学员关系管理系统(CRM)
----------------------------------------------------------------------------------------------[版权申明: ...
- Programming In Scala笔记-第九章、控制抽象
本章主要讲解在Scala中如何使用函数值来自定义新的控制结构,并且介绍Curring和By-name参数的概念. 一.减少重复代码 1.重复代码的场景描述 前面定义的函数,将实现某功能的代码封装到一起 ...
- Oracle11g R2创建PASSWORD_VERIFY_FUNCTION对应密码复杂度验证函数步骤
Oracle11g R2创建PASSWORD_VERIFY_FUNCTION对应密码复杂度验证函数步骤 运行测试环境:数据库服务器Oracle Linux 5.8 + Oracle 11g R2数据库 ...
- [csdn markdown]使用摘记三 简便快捷的流程图
在线编写文字就可以实现复杂的流程图,再也不需要纠结了! 开始 操作流程 条件 结束 开始 st=>start: 开始 操作流程 st->op->cond 条件 cond=>co ...
- 反射 学习笔记之Class类的使用
1 java世界中万事万物皆对象,除了2个特殊情况 int float等这些基本数据类型,(但是也都有Integer和Float等封装类做了弥补) java staic定义的,它不是属于对象的,而是 ...