[LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Given 4 points: (1,2), (3,6), (0,0), (1,3).
The maximum number is 3.
LeeCode上的原题,可参见我之前的博客Invert Binary Tree 翻转二叉树。
解法一:
// Recursion
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void invertBinaryTree(TreeNode *root) {
if (!root) return;
TreeNode *tmp = root->left;
root->left = root->right;
root->right = tmp;
invertBinaryTree(root->left);
invertBinaryTree(root->right);
}
};
解法二:
// Non-Recursion
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void invertBinaryTree(TreeNode *root) {
if (!root) return;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* node = q.front(); q.pop();
TreeNode *tmp = node->left;
node->left = node->right;
node->right = tmp;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
};
[LintCode] Invert Binary Tree 翻转二叉树的更多相关文章
- lintcode :Invert Binary Tree 翻转二叉树
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...
- [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 ...
- 226. Invert Binary Tree 翻转二叉树
[抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- leetcode 226 Invert Binary Tree 翻转二叉树
大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...
- 第27题:Leetcode226: Invert Binary Tree反转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 思路 如果根节点存在,就交换两个子树的根节点,用递归 ...
- LeetCode Invert Binary Tree 反转二叉树
思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...
- 【easy】226. Invert Binary Tree 反转二叉树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- [LintCode] Identical Binary Tree 相同二叉树
Check if two binary trees are identical. Identical means the two binary trees have the same structur ...
随机推荐
- Three-js 创建第一个3D场景
1.一个场景至少需要的三种类型组件 相机/决定哪些东西将在屏幕上渲染 光源/他们会对材质如何显示,以及生成阴影时材质如何使用产生影响 物体/他们是在相机透视图里主要的渲染队形:方块.球体等 ...
- 第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。
使用 from lazy_object_proxy.utils import cached_property,使用这个装饰器. 由于官方的行数比较少,所以可以直接复制出来用自己的. class cac ...
- Hadoop教程(五)Hadoop分布式集群部署安装
Hadoop教程(五)Hadoop分布式集群部署安装 1 Hadoop分布式集群部署安装 在hadoop2.0中通常由两个NameNode组成,一个处于active状态,还有一个处于standby状态 ...
- 【Winform】自定义Messagebox
1.保持Msgbox的Icon 2.可以追加Checkbox,RadioBOx 下载
- MySQL --- 计算指定日期为当月的第几周
SET @d=NOW(); ; 啦啦啦
- UDP通信-UdpClient
static void Main(string[] args) { Console.WriteLine("发送端"); byte[] buffer = System.Text.En ...
- Go之获取Windows下文件是否隐藏
起初,做了个小程序,用来检测磁盘中所有的文件 package main import( "fmt" "io/ioutil" "os" ) v ...
- 关于openssl的编译与使用
关于openssl的编译与使用,可以参考这两往篇文章 http://blog.csdn.net/lazyclough/article/details/7456131 http://www.leaves ...
- Jackson(ObjectMapper)的简单使用(可转xml)
参考文章:http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html (原文章更详细哦,且有介绍xml与java对象的互转) 参考文章作 ...
- SQL SERVER数据库新认识的一些基础知识
最近要接触sql server的存储过程啦,在处理更加复杂的逻辑过程前,就来看一下这些sql的基础语法,感觉看啦一些复杂一点的sql语句,突然发现我是有多么的薄弱啊,所以在一些基础的语法上面我再重新整 ...