LeetCode——Invert Binary Tree
Description:
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
递归invert就好了。
/**
* 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)
return null;
invert(root);
return root; }
public static void invert(TreeNode root) {
if(root==null || (root.right == null && root.left==null)) {
return;
}
TreeNode rNode = root.right;
TreeNode lNode = root.left;
root.right = lNode;
root.left = rNode;
invert(root.left);
invert(root.right);
}
}
LeetCode——Invert Binary Tree的更多相关文章
- LeetCode—— Invert Binary Tree
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
- [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 ...
- LeetCode: Invert Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...
- LeetCode Invert Binary Tree 反转二叉树
思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...
- leetcode Invert Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- LeetCode —— Invert Binary Tree
struct TreeNode* invertTree(struct TreeNode* root) { if ( NULL == root ) { return NULL; } if ( NULL ...
- lc面试准备:Invert Binary Tree
1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
- Python解Leetcode: 226. Invert Binary Tree
leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...
随机推荐
- CentOS安装redmine 2后的简单配置
CentOS5.4安装redmine详细步骤 http://blog.csdn.net/leekwen/article/details/8516832 <<<<输出日志的配置& ...
- Golang 类型转换整理
1.整形到字符串: var i int = 1 var s string s = strconv.Itoa(i) 或者 s = FormatInt(int64(i), 10) 2.字符串到整形 var ...
- Kettle变量和自己定义java代码的实例应用
1 kettle.properties參数配置数据源连接和FTP连接 因为測试环境和生产环境中数据库连接FTP等配置会在部署过程中变更,所以预先定义成配置项,在配置文件里改动.这样測试和公布将会变得 ...
- mongo-查询(2)——比较/$in/$nin/$or/$not
mongo通find来查找文档.可以执行精确匹配和模糊匹配. 2. 模糊匹配 2.1比较 > $gt , >= $gte, < $lt, <= $lte, != $ne > ...
- dvi 中的内容居中
text-align:right; 文本居中 line-height:35px;*垂直居中*
- C# json字符串格式
[{\"MenId\":\"1\",\"MenName\":\"内容管理\",\"ParentId\" ...
- 关于SQL语句的一些注意事项
1.Into 表后要编辑-IntelliSense-刷新本地缓存 才能访问新表 2.Is null不是=null
- tensorflow学习笔记(10) mnist格式数据转换为TFrecords
本程序 (1)mnist的图片转换成TFrecords格式 (2) 读取TFrecords格式 # coding:utf-8 # 将MNIST输入数据转化为TFRecord的格式 # http://b ...
- linux 打包 解压 tar zip tgz
.tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)------------------------- ...
- CentOS查看登录用户以及踢出用户
查看登录用户,使用w命令 [root@lnmp ~]# w 18:51:18 up 35 min, 2 users, load average: 0.00, 0.00, 0.00 USER ...