当前位置:网站首页>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 ~
边栏推荐
- Ni9185 and ni9234 hardware settings in Ni Max
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
- Variables and constants
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
- Unity FAQ (I) lack of references
- Revit secondary development - link file collision detection
- Take full control! Create a "leading cockpit" for smart city construction
- Revit secondary development - project file to family file
- Revit secondary development - modify wall thickness
猜你喜欢
Line test - graphic reasoning -5- one stroke class
如何选择合适的自动化测试工具?
LeetCode144. Preorder traversal of binary tree
Blender exchange group, welcome to the water group ~
Ueeditor custom display insert code
. Net automapper use
UWA Q & a collection
Unity FAQ (I) lack of references
Leetcode206. Reverse linked list
Visual studio 2019 installation
随机推荐
Remember that a development is encountered in the pit of origin string sorting
Revit secondary development - Hide occlusion elements
详解全志V853上的ARM A7和RISC-V E907之间的通信方式
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
OpenGL configuration vs2019
JS number is insufficient, and 0 is added
「开源摘星计划」Loki实现Harbor日志的高效管理
Blender exchange group, welcome to the water group ~
Debezium series: support the use of variables in the Kill Command
Attitude estimation (complementary filtering)
Firefox browser installation impression notes clipping
Redis cluster installation
Line test - graphic reasoning - 4 - alphabetic class
Matplotlib quick start
LeetCode144. Preorder traversal of binary tree
Install mxnet GPU version
Common verification rules of form components -2 (continuously updating ~)
What does it mean to prefix a string with F?
Unity local coordinates and world coordinates
OpenGL homework - Hello, triangle