226. Invert Binary Tree

Easy

Invert a binary tree.

Example:

Input:

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

Output:

     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 f*** off.

package leetcode.easy;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class InvertBinaryTree {
public TreeNode invertTree1(TreeNode root) {
if (root == null) {
return null;
}
TreeNode right = invertTree1(root.right);
TreeNode left = invertTree1(root.left);
root.left = right;
root.right = left;
return root;
} public TreeNode invertTree2(TreeNode root) {
if (root == null) {
return null;
}
java.util.Queue<TreeNode> queue = new java.util.LinkedList<TreeNode>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
TreeNode temp = current.left;
current.left = current.right;
current.right = temp;
if (current.left != null) {
queue.add(current.left);
}
if (current.right != null) {
queue.add(current.right);
}
}
return root;
} @org.junit.Test
public void test1() {
TreeNode tn11 = new TreeNode(4);
TreeNode tn21 = new TreeNode(2);
TreeNode tn22 = new TreeNode(7);
TreeNode tn31 = new TreeNode(1);
TreeNode tn32 = new TreeNode(3);
TreeNode tn33 = new TreeNode(6);
TreeNode tn34 = new TreeNode(9);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = tn31;
tn21.right = tn32;
tn22.left = tn33;
tn22.right = tn34;
tn31.left = null;
tn31.right = null;
tn32.left = null;
tn32.right = null;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
System.out.println(tn11);
System.out.println(invertTree1(tn11));
} @org.junit.Test
public void test2() {
TreeNode tn11 = new TreeNode(4);
TreeNode tn21 = new TreeNode(2);
TreeNode tn22 = new TreeNode(7);
TreeNode tn31 = new TreeNode(1);
TreeNode tn32 = new TreeNode(3);
TreeNode tn33 = new TreeNode(6);
TreeNode tn34 = new TreeNode(9);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = tn31;
tn21.right = tn32;
tn22.left = tn33;
tn22.right = tn34;
tn31.left = null;
tn31.right = null;
tn32.left = null;
tn32.right = null;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
System.out.println(tn11);
System.out.println(invertTree2(tn11));
}
}

LeetCode_226. Invert Binary Tree的更多相关文章

  1. 【07_226】Invert Binary Tree

    Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary ...

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

  3. 226. Invert Binary Tree(C++)

    226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...

  4. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  5. C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...

  6. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  7. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

  8. 【Invert Binary Tree】cpp

    题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...

  9. &lt;LeetCode OJ&gt; 226. Invert Binary Tree

    226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...

随机推荐

  1. Python文件的读写操作

    Python文件的使用 要点:Python能够以文本和二进制两种形式处理文件. 1.文件的打开模式,如表1:  注意:使用open()函数打开文件,文件使用结束后耀使用close()方法关闭,释放文件 ...

  2. house买房原理,2019,第一版

    ,购买框架 1,通过自己的买房预算金额 和 pre-approval 确定你要的房屋总价, 估计到自己可以接受的房子,卖方也喜欢这样的买家,但不一定能拿全额贷款 2,pre-approval对信用分数 ...

  3. MAC OSX下终端通过NTLM验证,通过代理上网(花了一天时间才解决这个)

    MAC OSX下终端通过NTLM验证,通过代理上网 公司网络限制如下: 公司通过代理来控制内网用户访问外网的权限.用户名和密码为域用户,采用的验证方式是NTLM(用的是foreFront TMG) 遇 ...

  4. JQuery通过click事件获取当前点击对象的id,name,value属性等

    $(".test").click(function () { var val=$(this).attr("id"); })

  5. java学习笔记(2)注释、public lcass、class、标识符、字面值、变量

    java学习笔记(1)中说过了java的一些基础知识,发展史,特点,编译和运行过程,配置环境变量等,接下来开始介绍java的语法等只是点  关于java源程序中的注释: *什么是注释?注释的作用是什么 ...

  6. [洛谷 P4556] 雨天的尾巴

    传送门 Solution 线段树合并的入门题 lca可以在dfs的时候离线求(用并查集) 更新的点有每条链的两个端点,它们的lca和dad[lca] 为了节省空间,lca和dad[lca]的更新可以先 ...

  7. Leetcode84. 柱状图中最大的矩形(单调栈)

    84. 柱状图中最大的矩形 前置 单调栈 做法 连续区间组成的矩形,是看最短的那一块,求出每一块左边第一个小于其高度的位置,右边也同理,此块作为最短限制.需要两次单调栈 单调栈维护递增区间,每次不满足 ...

  8. 编译器错误 CS0540

    编译项目报错:包含类型不实现接口,CS0540 原因:试图在非派生自接口的类中实现接口成员. 解决方案: 删除接口成员的实现,或将接口添加到类的基类列表. 下面的两个示例生成 CS0540: 一. / ...

  9. mysql group by 报错 ,only_full_group_by 三种解决方案

    报错信息  Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'data ...

  10. Barman安装及备份PostgreSQL

    barman特点 零数据丢失备份.保证用户在只有一台备份服务器的情况下达到零数据丢失. 与备份服务器合作.允许备份服务器在与主服务器的流式复制不可用时,从barman获取wal文件. 可靠的监控集成. ...