Lintcode175-Revert Binary Tree-Easy
175. Invert Binary Tree
Invert a binary tree.
Example
Example 1:
Input: {1,3,#}
Output: {1,#,3}
Explanation:
1 1
/ => \
3 3
Example 2:
Input: {1,2,3,#,#,4}
Output: {1,3,2,#,4}
Explanation:
1 1
/ \ / \
2 3 => 3 2
/ \
4 4
Challenge
Do it in recursion is acceptable, can you do it without recursion?
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
public void invertBinaryTree(TreeNode root) {
if (root == null) {
return;
} TreeNode temp = root.right;
root.right = root.left;
root.left = temp;
invertBinaryTree(root.left);
invertBinaryTree(root.right);
}
}
Lintcode175-Revert Binary Tree-Easy的更多相关文章
- 【leetcode】Minimum Depth of Binary Tree (easy)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 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 ...
- 93. Balanced Binary Tree [easy]
Description Given a binary tree, determine if it is height-balanced. For this problem, a height-bala ...
- LeetCode:104 Maximum Depth of Binary Tree(easy)
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- Invert Binary Tree(easy)
1.直接把递归把左右子树翻转即可 AC代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tre ...
- [leetcode] 111.Mininum Depth of Binary Tree (Easy)
原题 寻找二叉树最短深度 这里用了dfs,beat 100%,4ms class Solution { public: int minDepth(TreeNode *root, int minNum ...
- [leetcode] 543. Diameter of Binary Tree (easy)
原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...
- [leetcode] 110. Balanced Binary Tree (easy)
原题链接 水题 深度搜索每一节点的左右深度,左右深度差大于1就返回false. class Solution { public: bool isBalanced(TreeNode *root) { b ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- LeetCode_226. Invert Binary Tree
226. Invert Binary Tree Easy Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: ...
随机推荐
- Oracle tablespace 创建表空间
定义: 表空间是一个逻辑概念,它的所有数据和结构信息都存储在一个或多个数据文件中,表空间属于数据库的一部分.数据库自带有几个表空间,如system,temp.一般系统将创建几个私用或业务的表空间. 模 ...
- vue 中的数据绑定
vue当中有个v-model, 是怎么实现的呢?其实是利用了$event. <div id="app"> <!-- 输入什么,就输出什么 --> <i ...
- [转] - Weiflow——微博机器学习框架
Weiflow--微博机器学习框架 本文从开发效率(易用性).可扩展性.执行效率三个方面,介绍了微博机器学习框架Weiflow在微博的应用和最佳实践. 在上期<基于Spark的大规模机器学习在微 ...
- super方法 调用父类的方法
描述 super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO).重复 ...
- 剑指offer——python【第43题】左旋转字符串
题目描述 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”abc ...
- 补充:MySQL经典45道题型
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 四个表的结构分别如表1-1的表(一)~表 ...
- linux主要目录
/:根目录,一般根目录下只存放目录,在 linux 下有且只有一个根目录,所有的东西都是从这里开始 当在终端里输入 /home ,其实是在告诉电脑,先从 / (根目录)开始,再进入到 home 目录/ ...
- java动态加载配置文件(申明:来源于网络)
java动态加载配置文件 地址:http://blog.csdn.net/longvs/article/details/9361449
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- CH 2401 - 送礼 - [折半DFS+二分]
题目链接:传送门 描述 作为惩罚,GY被遣送去帮助某神牛给女生送礼物(GY:貌似是个好差事)但是在GY看到礼物之后,他就不这么认为了.某神牛有N个礼物,且异常沉重,但是GY的力气也异常的大(-_-b) ...