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: ...
随机推荐
- windows系统-phpstudy升级mysql8.0.12安装教程及修改密码和安装注意事项
1.下载安装包,下载地址:mysql8.0.12 .如果你想要下载其它版本可以选择:mysql历史版本地址. 2.下载好,删除phpstudy的mysql目录.如果数据重要的,注意备份数据!同意把m ...
- Spring 注解配置(2)——@Autowired
版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5626365.html @Autowired 注释,它可以对类成员变 ...
- Centos7 修改系统时区timezone
Centos7 修改系统时区timezone 注意:修改Linux系统的时区以后,再安装jvm,jvm默认会使用系统的时区.如果系统时区设置错误,安装jvm后,再修改系统的时区,但jvm的时区仍然用不 ...
- 注意兼容浮点运算误差 0.7 + 0.1 ==0.8 为false
所以比较 汇总或者计算的时候注意确定精度0.7 + 0.1 ==0.8 换成 Math.abs(0.7 + 0.1 ==0.8)<0.0001参考下
- 最短路(SPFA)
SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...
- PAT甲级1057 Stack【树状数组】【二分】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...
- PHP面试大全 基础篇100道问题
2017年3月7日14:23:21 其实计划很久但是移植没时间去收集和处理弄成一个完整的文件 SVN地址: https://git.oschina.net/zxadmin/live_z 目前基础部分更 ...
- 转:ArcGIS API For JavaScript官方文档(二十)之图形和要素图层——①Graphics概述
原文地址:ArcGIS API For JavaScript官方文档(二十)之图形和要素图层——①Graphics概述 ArcGIS JavaScript API允许在地图上绘制graphic(图形) ...
- typescript interface 泛型
interface interface Obj { [index: string]: any; } class Person { name: string; } let obj: obj = { na ...
- jQuery 学习笔记(3)(内容选择器、attr方法、prop方法,类的操作)
内容选择器: 1.$("div:empty"): 空的div元素 2.$("div:parent"): 非空div元素 3.$("div:contai ...