当前位置:网站首页>字节跳动面试题之镜像二叉树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; }
}
输出结果:
边栏推荐
- 语句加锁分析
- 【R语言】交互作用 测试数据
- Use baidu EasyDL intelligent bin
- 普罗米修斯原理及节点发布
- C# 利用iTextSharp画PDF
- The solution that does not work and does not take effect after VScode installs ESlint
- Deep Learning - Principles of Neural Networks 2
- 22 high mid term paper topics forecast
- 像天才一样思考:如何培养自己的创造力?
- INSTALL_RPATH and BUILD_RPATH problem in CMake
猜你喜欢
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
锁执行的过程
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disab
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
GNNExplainer applied to node classification task
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
vs番茄助手的方便功能和便捷快捷键介绍
db.sqlite3没有“as Data Source“解决方法
MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
随机推荐
工控设备的系统如何进行加固
C# 利用iTextSharp画PDF
【R语言】把文件夹下的所有文件提取到特定文件夹
报错:flask: TypeError: ‘function‘ object is not iterable
tidb crash test
中英文说明书丨TRC D-阿卓糖(D-Altrose)
[HNOI2002]营业额统计
Flask failed to create database without error
el-table缓存数据
BeautifulSoup4的介绍与使用
[R language] interaction test data
Error: flask: TypeError: 'function' object is not iterable
Xilinx Zynq ZynqMP DNA
[R language] Extract all files under a folder to a specific folder
pdf加密、找回密码
Simple Factory Pattern
【R语言】对文件进行归一化整理到各文件类型文件夹
GNNExplainer应用于节点分类任务
TCP segment of a reassembled PDU
workbench 数据导出