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. webpack起服务器报JavaScript heap out of memory

    配置如下: { "scripts": { "start": "node --max_old_space_size=4096 node_modules/ ...

  2. HDU6072 Logical Chain

    题意:动态修改图 \(G\) 的边集,求每次修改后的 \(\sum c\times (c−1) / 2\) (记每个强连通分量中的点数量为 \(c\) ).其中修改操作共 \(m\) 次,每次最多改 ...

  3. spark yarn 提交作业

    spark提交作业命令: ./spark-submit --master yarn --deploy-mode cluster --class com.zjlantone.hive.SparkOper ...

  4. QSetting介绍

    简介 QSettings类提供了持久的跨平台应用程序设置. 用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表,OS X和iOS的属性列表文件中. ...

  5. Linux中三种SCSI target的介绍之STGT

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/scaleqiao/article/deta ...

  6. C 指针常量 和常量指针 指向常量的指针常量的使用

    #include <stdio.h> /* 指针常量 和常量指针 指向常量的指针常量 */ int main() { int a = 100; int b =200; int* const ...

  7. mysql in()后子查询优化

    线上数据发现一条数据大量等待的现象,通过explain发现这个sql写法存在问题,这里简单记录一下. 业务场景是这样: 存在购物车和费用两张表,购物车数据是购买商品时生成,用于记录购买商品数据,同时购 ...

  8. FLUENT质量加权平均和面积加权平均的区别【转载】

    转载自:http://blog.sina.com.cn/s/blog_7ef78d170101bhfn.html 网上关于fluent中质量加强平均(Mass-Weighted Average)和面积 ...

  9. mysql 使用的三个小技巧

    mysql 使用的三个小技巧 快速阅读 Mysql查询工具中如何查询多条语名,Mysql中如何设置变量,Mysql中如何查特定字段,后面再加* Mysql查询工具中如何查询多条语名 默认myslq只能 ...

  10. BDD本质及与ATDD区别

    说起BDD,你会想到什么?   在刚接触BDD(Behavior Driven Development,行为驱动开发)的时候,我以为就是用Cucumber这样的工具来编写场景用例,从而实现自动化测试, ...