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的更多相关文章

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

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

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

  6. Leetcode 226 Invert Binary Tree python

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

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

  8. LeetCode 226 Invert Binary Tree 解题报告

    题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...

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

随机推荐

  1. (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。

    关于多线程执行显示进度条的实例! 之前回答了一篇关于怎么在线程中操作进度条的帖子,估计有人看的不是很明白今天没事,写了一个小小的实例,很简单,就2个文件权当抛砖引玉,希望有更好解决方案的人发表一下意见 ...

  2. Why doesn't Genymotion run on Windows 10?

    To date, VirtualBox is not yet fully compatible with Windows 10. As Genymotion relies on the use of ...

  3. Format可能存在的坑

    import java.text.MessageFormat; public class TestFormat { public static void main(String[] args) { / ...

  4. 【linux】umask

    1.默认情况下的umask值是022 [root@andon ~]# umask 0022  2.umask作用 默认文件权限:666(linux创建文件默认无执行权限)-umask =644(6-0 ...

  5. linux杂谈

    1. 目录的stick位 一般情况下,如果一个用户对一个目录有写权限,那么他就可以删除该目录下的文件,即使这些文件不是他的.为了防止这种情况,我们需要为目录设置stick位: chmod a+t yo ...

  6. How to install Wordpress 4.0 on CentOS 7.0

    This document describes how to install and configure Wordpress 4.0 on CentOS 7.0. WordPress started ...

  7. hist和bar画图关系

    1.hist是绘制直方图,直方图显示了数据值的分布情况.  1>n = hist(Y,n)      将向量Y中的元素分到n个等间隔的范围内(默认为10个间隔),并返回每个范围内元素的个数作为一 ...

  8. txt用Itunes同步到IPhone上

    纯水的LGF160s换了IPhone 5,想把原来txt的文件拷到手机上.百度只是有老版本的,最新也是11的.现在用12.06版的,菜单已经不太一样.找了半天,分享一下.

  9. windows7使用Source insight上远程修改ubuntu共享内核源码

    由于本人阅读喜欢使用source insight.前段时间接触了linux核代码,而这份代码只能放在ubuntu服务器上编译,刚开始的时候是在windows上修改,完了之后再copy到服务器上去编译, ...

  10. HTTP协议的安全性--全站HTTPS

    HTTP Basic Authentication很容易让攻击者监听并获取用户名密码.使用Base64来encode用户名密码也只是为将用户名和口令中的不兼容字符转换为均与HTTP协议兼容的字符集. ...