当前位置:网站首页>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 ~
边栏推荐
- Failed to initialize rosdep after installing ROS
- 行测-图形推理-3-对称图形类
- 微服务架构开源框架详情介绍
- Welcome to CSDN markdown editor
- Px4 autonomous flight
- Cannot find module 'xxx' or its corresponding type declaration
- [interview arrangement] 0211 game engine server
- Leetcode interview question 02.07 Linked list intersection [double pointer]
- JS number is insufficient, and 0 is added
- Attitude estimation (complementary filtering)
猜你喜欢
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
0-5vac to 4-20mA AC current isolated transmitter / conversion module
IP network active evaluation system -- x-vision
Leetcode interview question 02.07 Linked list intersection [double pointer]
Gazebo import the mapping model created by blender
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
行测-图形推理-8-图群类
Common verification rules of form components -2 (continuously updating ~)
Blender exchange group, welcome to the water group ~
随机推荐
7-18 simple simulation of banking business queue
Matplotlib快速入门
OpenGL jobs - shaders
Aspose. Word operation word document (II)
. Net automapper use
Line test - graphic reasoning - 3 - symmetric graphic class
Basic knowledge of binary tree
Line test - graphic reasoning - 4 - alphabetic class
行测-图形推理-4-字母类
Leetcode interview question 02.07 Linked list intersection [double pointer]
Select sort (illustration +c code)
Revit secondary development - modify wall thickness
ASP. Net core introduction V
Amesim2016 and matlab2017b joint simulation environment construction
Aspose. Words merge cells
Redis cluster installation
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
What does it mean to prefix a string with F?
VTOL in Px4_ att_ Control source code analysis [supplement]
The PHP source code of the new website + remove authorization / support burning goose instead of pumping