当前位置:网站首页>ByteDance Interview Questions: Mirror Binary Tree 2020

ByteDance Interview Questions: Mirror Binary Tree 2020

2022-08-09 06:32:00 In the history of the strongest disciple

Please complete a function that takes as input a binary tree and the function outputs its mirror image.

For example enter:

4
/ / \
2 7
/ \ / \
1 3 6 9
Mirror output:

4
/ / \
7 2
/ \ / \
9 6 3 1

Example:

Input: root = [4,2,7,1,3,6,9]Output: [4,7,2,9,6,3,1]

The idea of ​​solving the problem is the breadth-first traversal of the tree. There is also a data exchange problem, which is an extension of the data exchange problem of array data blocks. If you are interested, you can go to see Zuo Shen's A B A BA exchange problem.

import java.util.ArrayList;import java.util.List;public class test7 {public TreeNode mirrorTree(TreeNode root) {if(root == null){return root;}List list = new ArrayList<>();list.add(root);while (list.size()>0){List newList = new ArrayList<>();for(int i = 0;i

Output result:

原网站

版权声明
本文为[In the history of the strongest disciple]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090629203955.html