当前位置:网站首页>Sword finger offer 27 Image of binary tree
Sword finger offer 27 Image of binary tree
2022-07-07 22:52:00 【Yes' level training strategy】
subject : Please complete a function , Enter a binary tree , This function outputs its image .
For example, the input :
airplay mirroring :
Example 1:
Input :root = [4,2,7,1,3,6,9]
Output :[4,7,2,9,6,3,1]
The subject is not difficult to understand , Recursively exchange two positions , The most direct idea is to use recursion :
The code is as follows :
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode left = mirrorTree(root.left);
TreeNode right = mirrorTree(root.right);
root.left = right;
root.right = left;
return root;
}
}
Time complexity :O(n)
Spatial complexity :O(n)
It should be possible to do recursion after a few times , So this is also a simple problem .
Of course, recursion can be realized by stack , So let's take a look at the code implementation of the auxiliary stack :
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if (root == null) {
return null;
}
Deque<TreeNode> nodeStack = new ArrayDeque(); // stack Recommend to use Deque
nodeStack.add(root);
while (!nodeStack.isEmpty()) {
TreeNode node = nodeStack.removeLast();
if (node.right != null) {
nodeStack.add(node.right);
}
if (node.left != null) {
nodeStack.add(node.left);
}
TreeNode tempNode = node.left;
node.left = node.right;
node.right = tempNode;
}
return root;
}
}
Time complexity :O(n)
Spatial complexity :O(n)
Okay , This is a classic algorithm problem , There is a high probability of being asked , Need to master .
For more articles, you can click the business card below to pay attention ~
边栏推荐
- Line test - graphic reasoning - 4 - alphabetic class
- OpenGL job coordinate system
- Record layoutrebuild Forcerebuildlayoutimmediate does not take effect
- Leetcode interview question 02.07 Linked list intersection [double pointer]
- Revit secondary development - operation family documents
- Debezium series: introducing support for the final operator
- 行测-图形推理-2-黑白格类
- 「开源摘星计划」Loki实现Harbor日志的高效管理
- Unity local coordinates and world coordinates
- XMIND mind mapping software sharing
猜你喜欢
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
What does it mean to prefix a string with F?
详解全志V853上的ARM A7和RISC-V E907之间的通信方式
「开源摘星计划」Loki实现Harbor日志的高效管理
OpenGL configuration vs2019
Ueeditor custom display insert code
Basic knowledge of binary tree
Blender exchange group, welcome to the water group ~
VTOL in Px4_ att_ Control source code analysis [supplement]
随机推荐
行测-图形推理-8-图群类
Revit secondary development - intercept project error / warning pop-up
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
Line test - graphic reasoning - 6 - similar graphic classes
Unity technical notes (I) inspector extension
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
ASP.NET Core入门五
Redis官方ORM框架比RedisTemplate更优雅
UWA Q & a collection
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
Force deduction - question 561 - array splitting I - step by step parsing
Line test - graphic reasoning - 4 - alphabetic class
行測-圖形推理-4-字母類
Revit secondary development - Hide occlusion elements
如何选择合适的自动化测试工具?
Debezium系列之:引入对 LATERAL 运算符的支持
How to choose the appropriate automated testing tools?
7-18 simple simulation of banking business queue