Invert Binary Tree

Invert a binary tree.

     4
/ \
2 7
/ \ / \
1 3 6 9

to

     4
/ \
7 2
/ \ / \
9 6 3 1
 /**
* 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 root;
}else{
TreeNode n = root.left;
root.left = invertTree(root.right);
root.right = invertTree(n);
return root;
}
}
}

LeetCode 226的更多相关文章

  1. 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 ...

  2. Java实现 LeetCode 226 翻转二叉树

    226. 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max ...

  3. 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): ...

  4. 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 ...

  5. [leetcode 226] Invert Tree

    1 题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 2 思路: 这是因为谷歌面试xx而 ...

  6. (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 ...

  7. 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 解题思路: 我只想说递归大法好. ...

  8. Leetcode 226 Invert Binary Tree python

    题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...

  9. 10. leetcode 226 Invert Binary Tree

    思路:递归.先将左子树反转,再将右子树反转,然后让root->left指向反转后的右子树,root->right指向反转后的左子树.

随机推荐

  1. 第三百零二天 how can I 坚持

    今天给掌中宝提了几个bug,确实管用,哈哈. 还有就是弟弟买房了,海亮艺术公馆,还好,至少安定下来了,可惜啊,我看好的房子也有的卖了,咋办啊. 看准的东西总是会想法设法的买了,可是无能为力啊. 还有, ...

  2. codeforces Ebony and Ivory(水题)

    A. Ebony and Ivory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. Eclipse查找类路径快捷方式

    直接ctrl+shift+t查找这个类,下面会显示类的路径,包括jar名

  4. MySQL select into outfile用法

    select into outfile用法 SELECT ... FROM TABLE_A INTO OUTFILE "/path/to/file" FIELDS TERMINAT ...

  5. Python3批量爬取网页图片

    所谓爬取其实就是获取链接的内容保存到本地.所以爬之前需要先知道要爬的链接是什么. 要爬取的页面是这个:http://findicons.com/pack/2787/beautiful_flat_ico ...

  6. PostgreSQL的 initdb 源代码分析之二十

    继续分析: setup_privileges(); 展开: 这是设置权限. 其cmd是:"/home/pgsql/project/bin/postgres" --single -F ...

  7. php 算法之切割数组,不用array_chunk(),算法之二,取数组的差值,不用array_diff()

    用php写算法切割数组,不用array_chunk();算法例如以下所看到的. <?php //$array 数组 //$size 每一个数组的个数 //每一个数组元素是否默认键值 functi ...

  8. C++ 内存相关

    1.C++的内存管理可分为以下几个部分: 栈:记录程序的执行过程. 堆:采用new,delete申请释放内存. 自由存储区:对应于C中使用malloc,free申请释放内存. 全局存储区:也叫静态存储 ...

  9. Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分

    B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...

  10. Android通过http协议POST传输方式

    Android通过http协议POST传输方式如下: 方式一:HttpPost(import org.apache.http.client.methods.HttpPost) 代码如下: privat ...