(easy)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.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null || (root.left == null) && (root.right == null))
return root;
root=invertTreeLR(root,root.left, root.right);
return root;
} public TreeNode invertTreeLR(TreeNode root,TreeNode left, TreeNode right) { //left,right位临时引用
if (left == null && right != null) {
left = right;
right = null;
left=invertTreeLR(left,left.left, left.right);
} else if (left != null && right == null) {
right = left;
left = null;
right=invertTreeLR(right,right.left, right.right);
} else if (left == null && right == null){ } else {
TreeNode tmp = null;
tmp = left;
left = right;
right = tmp;
left=invertTreeLR(left,left.left, left.right);
right=invertTreeLR(right,right.left, right.right); }
root.left=left;
root.right=right;
return root;
}
}运行结果:
(easy)LeetCode 226.Invert Binary Tree的更多相关文章
- 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 ...
- 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
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 class Solution(object): ...
- Java for 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 wa ...
- Java [Leetcode 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
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- 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 ...
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
- 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 ...
随机推荐
- string 字符串的分隔处理与list的相互转换
在指定 String 数组的每个元素之间串联指定的分隔符 String,从而产生单个串联的字符串.(来源于MSDN) 有两个重载函数:[C#]public static string Join( ...
- iOS 下拉刷新和加载更多 (OC\Swift)
Swift语言出来之后, 可能还没有第三方的下拉刷新和上提加载, 所以自己用UIRefreshControl控件和UITableView实例的tableFooterView(底部视图)属性结合起来写了 ...
- Visual Studio个人常用快捷键
Ctrl+F5:运行程序 F9:设置/取消断点 F5:启动调试 F10:逐过程单步调试 F11:逐语句单步调试 按住Ctrl先按K再按D:格式化全部代码 按住Ctrl先按K再按F:将选中代码块格式化 ...
- FileReader 基本介绍
转自:http://blog.csdn.net/zk437092645/article/details/8745647 用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步 ...
- Eclipse给方法分配足够的内存
junit测试 VM parameters -Xmx1024M -XX:PermSize=128m -XX:MaxPermSize=256m
- php生成二维码可以有以下几种方式
1.google开放api $urlToEncode="http://bbs.lewanchina.com"; generateQRfromGoogle($urlToEncode) ...
- js cookie操作
//写Cookie function writeCookie(name, value) { var expire = new Date(); expire.setFullYear(expire.get ...
- css3之transform的应用
一.利用transform实现图片额外显示 效果图如下 初始状态:
- 【Spring学习笔记-6】关于@Autowired与@Scope(BeanDefination.SCOPE_PROTOTYPE)
当类被@Scope(BeanDefination.SCOPE_PROTOTYPE)修饰时,说明每次依赖注入时,都会产生新的对象,具体可参见文章:http://blog.csdn.net/gst6062 ...
- MySQL加载并执行SQL脚本文件
第一种方法: 命令行下(未连接数据库) ,输入 mysql -h localhost -u root -p123456 < C:\db.sql 第二种方法: 命令行下(已连接数据库,此时的提示符 ...