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. jquery刷新数据随机排列

    jquery 随机排列<li> 在线等 急... ul里 有六个li 每次刷新 li 的顺序就随机排列一次 求大神指点..... 代码如下:不会插入代码 只能截图了 <!DOCTYP ...

  2. mysql模糊查询多个字段

    SELECT * FROM nst_t_conferencehis WHERE ConferName REGEXP '军工|军资';

  3. Phoenix 简单介绍

    转载自:https://blog.csdn.net/carolzhang8406/article/details/79455684 1. Phoenix定义 Phoenix最早是saleforce的一 ...

  4. DVWA-XSS练习

    本周学习内容: 1.学习web应用安全权威指南: 2.学习乌云漏洞: 实验内容: DVWA实验XSS跨站脚攻击 实验步骤: Low 1.打开DVWA,进入DVWA security模块,将难度修改为L ...

  5. learning java Runtime 类 获取内存及处理器核数信息

    var rt = Runtime.getRuntime(); System.*)); System.*)); System.*)); System.out.println(rt.availablePr ...

  6. (2)Angular的开发

    什么是 ReactNative应用, 小程序与RN的关系 native端优化 Js端优化 ReactNative是? 用JavaScript来写App,性能好 异步编程 请点赞!因为你的鼓励是我写作的 ...

  7. LeetCode算法题-链表类

    1.将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. (可以参照第2的merge2List实现) 示例: 输入:1->2->4, 1->3 ...

  8. 2019暑期金华集训 Day5 树上数据结构

    自闭集训 Day5 树上数据结构 前置知识 点分治 边分治 树链剖分 LCT Top Tree LCT时间复杂度 线段树每次查询是严格\(\log n\)的,然而splay维护连续段的时候,如果每次查 ...

  9. 《挑战30天C++入门极限》C/C++中字符串常量的不相等性及字符串的Copy

        C/C++中字符串常量的不相等性及字符串的Copy #include <iostream>    void main(void)  {      if("test&quo ...

  10. 《挑战30天C++入门极限》新手入门:C++中的函数重载

        新手入门:C++中的函数重载 函数重载是用来iostream>  using namespace std;  int test(int a,int b);  float test(flo ...