算法:Invert Binary Tree(翻转二叉树)
说明
算法:Invert Binary Tree
LeetCode地址:https://leetcode.com/problems/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.
解题思路1
翻转二叉树,深度优先遍历,用递归的方式翻转,注意到层级一致来调换,所以先把递归调用写在前面,最后再用交换。
写二叉树比较容易,校验代码如何写。笔者按照例子入参,改造为TreeNode对象,再改写TreeNode的 ToString()
方法,把输入和输出的二叉树都已数组的方式打印出来。画二叉树的规则是,逐层从上到下画出即可。
时间复杂度为 O(N), 数据都会遍历一遍,时间复杂度跟层数成正比,复杂度在log(n) ~ n 之间。
代码实现1
import java.util.LinkedList;
import java.util.Queue;class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x; }@Overridepublic String toString() {Queue<TreeNode> queue = new LinkedList<>();queue.add(this);StringBuilder sb = new StringBuilder();sb.append("[");while (!queue.isEmpty()) {TreeNode current = queue.poll();sb.append(current.val).append(", ");if (current.left != null) queue.add(current.left);if (current.right != null) queue.add(current.right);}sb.append("]");return sb.toString();}
}public class InvertBinaryTree {public TreeNode invertTree(TreeNode root) {if (root == null) {return root;}TreeNode left = invertTree(root.left);TreeNode right = invertTree(root.right);root.left = right;root.right = left;return root;}public static TreeNode initData() {int[] data = {4, 2, 7, 1, 3, 6, 9};TreeNode root = new TreeNode(4);Queue<TreeNode> queue = new LinkedList<>();queue.add(root);for (int i = 1; i < data.length; ) {TreeNode currentRoot = queue.poll();TreeNode left = new TreeNode(data[i]);currentRoot.left = left;queue.add(currentRoot.left);if (i + 1 < data.length) {TreeNode right = new TreeNode(data[++i]);currentRoot.right = right;queue.add(currentRoot.right);}i++;}return root;}public static void main(String[] args) {TreeNode root = initData();System.out.println("Input: " + root.toString());InvertBinaryTree obj = new InvertBinaryTree();obj.invertTree(root);System.out.println("Output: " + root.toString());}}
运行结果1
Input: [4, 2, 7, 1, 3, 6, 9, ]
Output: [4, 7, 2, 9, 6, 3, 1, ]
代码执行效率1
Runtime: 0 ms, faster than 100.00% of Java online submissions for Invert Binary Tree.
Memory Usage: 35.7 MB, less than 29.71% of Java online submissions for Invert Binary Tree.
解题思路2
用递归的方式实现的风险是,方法指针栈会导致栈溢出,所以改为内存栈的方式,按照广度优先遍历的方式处理。
时间复杂度为 O(N)。
代码实现2
import java.util.LinkedList;
import java.util.Queue;public class InvertBinaryTree {public TreeNode invertTreeWithBreadthSearchFirst(TreeNode root) {if (root == null) {return root;}Queue<TreeNode> queue = new LinkedList<>();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;}}
运行结果2
Input: [4, 2, 7, 1, 3, 6, 9, ]
Output: [4, 7, 2, 9, 6, 3, 1, ]
代码执行效率2
Runtime: 0 ms, faster than 100.00% of Java online submissions for Invert Binary Tree.
Memory Usage: 35.8 MB, less than 6.82% of Java online submissions for Invert Binary Tree.
总结
翻转二叉树有两种方式,一种是深度优先遍历,第二种是广度优先遍历。
代码下载:
https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/popular/InvertBinaryTree.java
算法:Invert Binary Tree(翻转二叉树)相关推荐
- 226. Invert Binary Tree 翻转二叉树
翻转一棵二叉树. 示例: 输入: 4/ \2 7/ \ / \ 1 3 6 9 输出: 4/ \7 2/ \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell 的 原问题 启发的 ...
- [LeetCode][JavaScript]Invert Binary Tree 反转二叉树
反转二叉树 其实我从没有想到前端面试会问到这个问题,题目来源于google的面试 Google: 90% of our engineers use the software you wrote (Ho ...
- [LeetCode] Invert Binary Tree - 二叉树翻转系列问题
目录: 1.Invert Binary Tree - 二叉树翻转 [递归] 题目概述: Invert a binary tree. 4/ \2 7/ \ / \ 1 3 6 9 to 4/ \7 2/ ...
- java实现翻转二叉树_【leetcode刷题】[简单]226. 翻转二叉树(invert binary tree)-java...
翻转二叉树 invert binary tree 题目 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 ...
- 226. Invert Binary Tree 1
题目链接:Invert Binary Tree 思路: 如果需要反转一个二叉树,那么我们需要遍历整个树的所有节点. 如果想遍历所有的节点,我们可以用Depth First Search(DFS)或者B ...
- 领扣LintCode算法问题答案-175. 翻转二叉树
领扣LintCode算法问题答案-175. 翻转二叉树 目录 175. 翻转二叉树 鸣谢 175. 翻转二叉树 翻转一棵二叉树.左右子树交换. 样例 1: 输入: {1,3,#} 输出: {1,#,3 ...
- LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法
题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...
- leetcode(226)—— Invert Binary Tree(Python/C++)
Invert Binary Tree 二叉树节点定义: struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) ...
- C#LeetCode刷题之#226-翻转二叉树(Invert Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4080 访问. 翻转一棵二叉树. 输入: 4 / \ ...
- leetcode python3 简单题226. Invert Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十六题 (1)题目 英文: Invert a binary tree. 中文 ...
最新文章
- 微软华人团队刷新COCO记录!全新目标检测机制达到SOTA|CVPR 2021
- 操作系统内核(linux)
- js 对象数组常用操作 我用到的
- MySQL8.0.14 - 新特性 - InnoDB Parallel Read简述
- 利用这10个工具,你可以写出更好的Python代码
- VARCHART XGantt甘特图具有更多功能的HTML5 / Gantt图表的可视计划小部件
- python 爬虫代码实例
- 基于STM32f103的TM1640驱动程序(地址自动加1 和 固定地址)
- java下载文件接口
- Matlab的自相关函数corr
- MySQL 8.0.27 下载安装与配置详细教程(Windows64位)
- 悉尼科技大学量子计算_世界排名前12位的量子计算研究型大学
- 混合柯西变异和均匀分布的蝗虫优化算法-附代码
- 链路聚合负载分担方式
- 专访百度资深工程师孙源:代码强迫症的死实践派
- 外企面试,哪有你想象的那么难!
- 位运算符——左移、右移
- css渐变效果的实现
- 操作系统与网络 2019-1-26
- crc16 ibm c语言,CRC16常见几个标准的算法及C语言实现