当前位置:网站首页>字节跳动面试题之镜像二叉树2020
字节跳动面试题之镜像二叉树2020
2022-08-09 06:29:00 【史上最强的弟子】
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
例如输入:
4
/ \
2 7
/ \ / \
1 3 6 9
镜像输出:
4
/ \
7 2
/ \ / \
9 6 3 1
实例:
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]解题思路是树的广度优先遍历,这里还有一个数据交换问题,是数组数据块数据交换问题的延伸题目,有兴趣的可以去看看左神的 A B A BA 交换问题。
import java.util.ArrayList;
import java.util.List;
public class test7 {
public TreeNode mirrorTree(TreeNode root) {
if(root == null){
return root;
}
List<TreeNode> list = new ArrayList<>();
list.add(root);
while (list.size()>0){
List<TreeNode> newList = new ArrayList<>();
for(int i = 0;i<list.size();i++) {
TreeNode treeNode = list.get(i);
TreeNode node3 = treeNode.left;
treeNode.left = treeNode.right;
treeNode.right = node3;
if(treeNode.left!=null){
newList.add(treeNode.left);
}
if(treeNode.right!=null){
newList.add(treeNode.right);
}
}
list = newList;
}
return root;
}
public static void main(String[] args) {
test7 test7 = new test7();
TreeNode treeNode1 = new TreeNode(4);
TreeNode treeNode2 = new TreeNode(2);
TreeNode treeNode3 = new TreeNode(7);
TreeNode treeNode4 = new TreeNode(1);
TreeNode treeNode5 = new TreeNode(3);
TreeNode treeNode6 = new TreeNode(6);
TreeNode treeNode7 = new TreeNode(9);
treeNode1.left =treeNode2;
treeNode1.right = treeNode3;
treeNode2.left = treeNode4;
treeNode2.right = treeNode5;
treeNode3.left = treeNode6;
treeNode3.right = treeNode7;
TreeNode treeNode = test7.mirrorTree(treeNode1);
System.out.println(treeNode);
}
}
class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x) { val = x; }
}
输出结果:

边栏推荐
- Ferric oxide/bismuth sulfide nanocomposites ([email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop
- C language implements sequential stack and chain queue
- Unity C# 委托——事件,Action,Func的作用和区别
- Unity五子棋游戏设计 和简单AI实现(1)
- Unity 五子棋游戏设计和简单AI(2)
- 【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
- 工控设备的系统如何进行加固
- 中英文说明书丨CalBioreagents 醛固酮单克隆抗体
- [GO], arrays and slices
- vs番茄助手的方便功能和便捷快捷键介绍
猜你喜欢

Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine

Deep Learning - Principles of Neural Networks 2

IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用

Redis 2 - 高级

代码目录结构

报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道

redis 运行lua 脚本 出现Invalid argument(s)

Fragments

Unity C# 委托——事件,Action,Func的作用和区别

db.sqlite3 has no "as Data Source" workaround
随机推荐
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道
idea中PlantUML插件使用
GNNExplainer applied to node classification task
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disab
Remember a nest.js route that matches all the path problems that follow
DevNet: Deviation Aware Networkfor Lane Detection
Used to import the data type
sql问题解答创建表的语句
install flask
tidb crash test
db.sqlite3 has no "as Data Source" workaround
sql problem solving statement to create a table
详解C语言中的wait()函数和waitpid()函数
Service
工控设备的系统如何进行加固
6 states of a thread
jdepend
[R language] interaction test data
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
untiy countdown