Lintcode---翻转二叉树
翻转一棵二叉树
1 1
/ \ / \
2 3 => 3 2
/ \
4 4
思路:依旧采用递归的思路,判断特殊条件后,先交换根节点的左右孩子,然后再对其左右子树进行递归调用。
代码实现也很简单,一次AC。
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
/*
思路:还是采用递归的方法,交换根节点的左右孩子,然后再对左右子树进行递归调用。
*/
void invertBinaryTree(TreeNode *root) {
// write your code here
if(root==NULL){
return;
} if(root->left==NULL&&root->right==NULL){
return;
} TreeNode* temp=root->right;
root->right=root->left;
root->left=temp; if(root->right!=NULL){
invertBinaryTree(root->right);
}
if(root->left!=NULL){
invertBinaryTree(root->left);
}
}
};
Lintcode---翻转二叉树的更多相关文章
- lintcode :Invert Binary Tree 翻转二叉树
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...
- [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] 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 ...
- [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 ...
- leetcode 翻转二叉树
翻转二叉树的步骤: 1.翻转根节点的左子树(递归调用当前函数) 2.翻转根节点的右子树(递归调用当前函数) 3.交换根节点的左子节点与右子节点 class Solution{ public: void ...
- 【leetcode 简单】 第六十四题 翻转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell的 原问题 ...
- LeetCode:翻转二叉树【226】
LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...
- 领扣(LeetCode)翻转二叉树 个人题解
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题 ...
- 翻转二叉树(深搜-先序遍历-交换Node)
题目:翻转二叉树,例如 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 已知二叉树的节点定义如下: class TreeNode { in ...
- 算法 翻转二叉树 dfs
翻转二叉树 翻转一棵二叉树.左右子树交换. Example 样例 1: 输入: {1,3,#} 输出: {1,#,3} 解释: 1 1 / => \ 3 3 样例 2: 输入: {1,2,3,# ...
随机推荐
- 再见了,DM
在DM奋斗了20个月之后,我终于有机会DM说再见.这我不是我第一次和DM说再见,因此我也不确定这次的再见是再也不见,还是再次见面.但有一点可以确定的是,在接下来相当长的一段时间内,我是没有机会 ...
- sourceinsight 工程和源码不在同一个盘符下
建立sourceinsight的时候,si工程可以和项目源码不在同一个盘下面,即si工程在D盘下,而阅读的源码在E盘下. 方法步骤如下: 下看一下目录结构: Y:\work\Hi3521\Hi3521 ...
- .NET:在C#中模拟Javascript的setTimeout方法
背景 每种语言都有自己的定时器(Timer),很多人熟悉Javascript中的setInterval和setTimeout,在Javascript中为了实现平滑的动画一般采用setTimeout模拟 ...
- Xcode 安装插件管理器
https://github.com/alcatraz/Alcatraz运行之后, load bundle, 然后window就有pakagegemanage. 下载如下插件: 自动导入插件
- Out of memory: Kill process 内存不足
服务直接被 killed,感觉特别奇怪.代码肯定是没有问题的,但为什么放到服务器上就出错了呢. 部署时报错如下: Failed to add the deployment content to the ...
- OpenJudge 8782 乘积最大——S.B.S
8782:乘积最大 总时间限制: 1000ms 内存限制: 65536kB 描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江 ...
- C#常见算法题目
//冒泡排序 public class bubblesorter { public void sort(int[] list) { ...
- webshell三剑客[aspxspy、phpspy、jspspy]
ASPSPY:http://www.rootkit.net.cn/article.asp?id=132<已关闭> 下载:ASPXspy2 JSPSPY:http://www.forjj.c ...
- 各浏览器CSS兼容问题
CSS对浏览器的兼容性有时让人很头疼,或许当你了解当中的技巧跟原理,就会觉得也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理方法并 整理了一下.对于web2.0的过度,请尽量用xhtml ...
- mysql的数据恢复
转载自:http://ourmysql.com/archives/1293 数据库数据被误删除是经常看到的事情,数据的恢复也就自然成为了DBA很重要的一门基本功夫,比较笨拙的办法是拉出历史的备份到另外 ...