当前位置:网站首页>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 ~
边栏推荐
- 全面掌控!打造智慧城市建设的“领导驾驶舱”
- Matplotlib quick start
- Redis official ORM framework is more elegant than redistemplate
- C # Development -- pit encountered in JS intermodulation
- Build an "immune" barrier in the cloud to prepare your data
- Get the week start time and week end time of the current date
- C # realizes the communication between Modbus protocol and PLC
- How to choose the appropriate automated testing tools?
- Ren Qian code compilation error modification
- Remember an experience of using selectmany
猜你喜欢

Add get disabled for RC form

Ni9185 and ni9234 hardware settings in Ni Max

How to choose the appropriate automated testing tools?

行测-图形推理-3-对称图形类

Early childhood education industry of "screwing bar": trillion market, difficult to be a giant

C # realizes the communication between Modbus protocol and PLC

XMIND mind mapping software sharing

vite Unrestricted file system access to

Common verification rules of form components -2 (continuously updating ~)

数字化转型:五个步骤推动企业进步
随机推荐
微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
PHP records the pitfalls encountered in the complete docking of Tencent cloud live broadcast and im live group chat
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
Form组件常用校验规则-2(持续更新中~)
Debezium series: set role statement supporting mysql8
Firefox browser installation impression notes clipping
. Net automapper use
Ni9185 and ni9234 hardware settings in Ni Max
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Attitude estimation (complementary filtering)
Two methods of calling WCF service by C #
Revit secondary development - collision detection
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
Details of the open source framework of microservice architecture
Revit secondary development - get the project file path
Force deduction - question 561 - array splitting I - step by step parsing
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
Build an "immune" barrier in the cloud to prepare your data
JS number is insufficient, and 0 is added