LeetCode_226. Invert Binary Tree
226. Invert Binary Tree
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的更多相关文章
- 【07_226】Invert Binary Tree
Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary ...
- 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 ...
- 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 ...
- 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 ...
- C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- LeetCode—— Invert Binary Tree
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
随机推荐
- stm32的hal之串口库函数总结复习
1.串口的使用方法 在hal库中,有三个串口发送的函数 a.HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uin ...
- javascript单一复制粘贴
<div id="copy-txt">内容内容</div> <button type="submit" onclick=" ...
- 项目读取数据,一直出现 Closing connections idle longer than 30 SECONDS,卡死现象
项目读取数据,一直出现 Closing connections idle longer than 30 SECONDS,卡死现象. 我的是在读取oracle数据的时候出现这种错误. 可以参考这篇文章 ...
- 软件测试技术之可用性测试之WhatsApp Web
Tag:可行性测试.测试流程.结果分析.案例分析 WhatsApp是一款面向智能手机的网络通讯服务,它可以通过网络传送短信.图片.音频和视频.WhatsApp在全球范围内被广泛使用,是最受欢迎的即时聊 ...
- Greenplum 添加mirror步骤
原文链接:https://yq.aliyun.com/articles/695864 [TOC] 概述 新安装的greenplum集群只有primary节点,没有mirror.高可用性没得到保证.所以 ...
- learning java identityHashCode
var S1 = new String("aaaaa"); System.out.println("SI hasCode: " + S1.hashCode()) ...
- fread和fwrite和feof读写二进制文件
#include <stdio.h> #include <stdlib.h> void text_to_bin(char *argv[]); void bin_to_text( ...
- python时间序列数据的对齐和数据库的分批查询
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 0. 前言 在机器学习里,我们对时间序列数据做预处理的时候,经常会碰到一个问题:有多个时间序列存在多个表里,每个表的的时间轴不完全相同,要如 ...
- C语言strncasecmp()函数:比较字符串的前n个字符
定义 int strncasecmp(const char *s1, const char *s2, size_t n); 描述 strncasecmp()用来比较参数s1 和s2 字符串前n个字符, ...
- 【微信小程序】scroll-view 的上拉加载和下拉刷新
1.在微信小程序中,想到 下拉刷新 和 上拉加载,如果是整个页面都拖动的话,可以在页面配置中,配置 enablePullDownRefresh 和 onReachBottomDistance 然后在 ...