当前位置:网站首页>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 ~
边栏推荐
猜你喜欢
Amesim2016 and matlab2017b joint simulation environment construction
Visual design form QT designer design gui single form program
行测-图形推理-6-相似图形类
Firefox browser installation impression notes clipping
Gazebo import the mapping model created by blender
CTF练习
ASP. Net core introduction V
PHP method of obtaining image information
Leetcode94. Middle order traversal of binary trees
Unity FAQ (I) lack of references
随机推荐
Typeorm automatically generates entity classes
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
Debezium系列之:引入对 LATERAL 运算符的支持
Revit secondary development - link file collision detection
Unity FAQ (I) lack of references
Revit secondary development - collision detection
Visual design form QT designer design gui single form program
Install mxnet GPU version
全面掌控!打造智慧城市建设的“领导驾驶舱”
Unity local coordinates and world coordinates
Leetcode interview question 02.07 Linked list intersection [double pointer]
Write in front -- Talking about program development
Cannot find module 'xxx' or its corresponding type declaration
OpeGL personal notes - lights
Debezium系列之:支持 mysql8 的 set role 語句
变量与常量
Aspose. Words merge cells
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
Remember aximp once Use of exe tool
Redis cluster installation