Easy-题目4:226. Invert Binary Tree

本文介绍了一种经典的二叉树翻转算法,并提供了一个简洁的C语言实现示例。通过对该问题的深入分析,文章展示了如何利用递归思想解决二叉树相关问题。

题目原文:
Invert a binary tree.

 4

/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
题目大意:
翻转一个二叉树。
琐事:
这个问题由HomeBrew软件的开发者Max Howell提出:
谷歌:虽然我们 90% 工程师都在用你写的软件(Homebrew),但你不能在白板上反转二叉树,所以滚蛋。
题目分析:
使用递归解法,对空二叉树直接退出,否则递归翻转左右子树并安到对应的位置上。
源码:(language:c)

struct TreeNode* invertTree(struct TreeNode* root) {
    if(!root)
        return NULL;
    struct TreeNode* temp = root->left;
    root->left = invertTree(root->right);
    root->right = invertTree(temp);
    return root;
}

成绩:
0ms,beats0%.众数:0ms.100%
Cmershen的碎碎念:
一开始拿到此题时,想想连Homebrew作者都不会的题,我能会么。后来考虑了很多情况,例如叶子节点,只有一个孩子的节点等情况,代码写了很长。但接下来发现所有情况可以归结为空与非空两种情况,所以写出上述的ac代码应该是足够简洁且易理解了。

Panels: - Class: rviz/Displays Help Height: 0 Name: Displays Property Tree Widget: Expanded: - /Global Options1 - /Status1 - /AllMap1/Autocompute Value Bounds1 - /ClosedNodes1 - /ClosedNodes1/Namespaces1 - /gridPath1 - /gridPath1/Namespaces1 - /Marker1 Splitter Ratio: 0.609022558 Tree Height: 585 - Class: rviz/Selection Name: Selection - Class: rviz/Tool Properties Expanded: - /3D Nav Goal1 Name: Tool Properties Splitter Ratio: 0.588679016 - Class: rviz/Views Expanded: - /Current View1 Name: Views Splitter Ratio: 0.5 - Class: rviz/Time Experimental: false Name: Time SyncMode: 0 SyncSource: AllMap Toolbars: toolButtonStyle: 2 Visualization Manager: Class: "" Displays: - Alpha: 0.5 Cell Size: 0.200000003 Class: rviz/Grid Color: 0; 0; 0 Enabled: true Line Style: Line Width: 0.0299999993 Value: Lines Name: Grid Normal Cell Count: 0 Offset: X: 0 Y: 0 Z: 0 Plane: XY Plane Cell Count: 200 Reference Frame: <Fixed Frame> Value: true - Class: rviz/Axes Enabled: true Length: 2 Name: Axes Radius: 0.0500000007 Reference Frame: <Fixed Frame> Value: true - Alpha: 1 Autocompute Intensity Bounds: true Autocompute Value Bounds: Max Value: 3.9000001 Min Value: 0 Value: false Axis: Z Channel Name: intensity Class: rviz/PointCloud2 Color: 193; 193; 193 Color Transformer: AxisColor Decay Time: 0 Enabled: true Invert Rainbow: false Max Color: 255; 255; 255 Max Intensity: 4096 Min Color: 0; 0; 0 Min Intensity: 0 Name: AllMap Position Transformer: XYZ Queue Size: 10 Selectable: true Size (Pixels): 3 Size (m): 0.200000003 Style: Boxes Topic: /demo_node/grid_map_vis Unreliable: false Use Fixed Frame: true Use rainbow: true Value: true - Class: rviz/Marker Enabled: false Marker Topic: /demo_node/closed_nodes_vis Name: ClosedNodes Namespaces: {} Queue Size: 100 Value: false - Class: rviz/Marker Enabled: false Marker Topic: /fast_flight_node/close_nodes_sequence_vis Name: closeNodesSequence Namespaces: {} Queue Size: 100 Value: false - Class: rviz/Marker Enabled: false Marker Topic: /fast_flight_node/open_nodes_vis Name: OpenNodes Namespaces: {} Queue Size: 100 Value: false - Class: rviz/Marker Enabled: true Marker Topic: /demo_node/grid_path_vis Name: gridPath Namespaces: {} Queue Size: 100 Value: true - Class: rviz/Marker Enabled: false Marker Topic: /demo_node/debug_nodes_vis Name: Marker Namespaces: {} Queue Size: 100 Value: false Enabled: true Global Options: Background Color: 255; 255; 127 Default Light: true Fixed Frame: world Frame Rate: 30 Name: root Tools: - Class: rviz/Interact Hide Inactive Objects: true - Class: rviz/MoveCamera - Class: rviz/FocusCamera - Class: rviz_plugins/Goal3DTool Topic: goal Value: true Views: Current: Class: rviz/Orbit Distance: 28.9093609 Enable Stereo Rendering: Stereo Eye Separation: 0.0599999987 Stereo Focal Distance: 1 Swap Stereo Eyes: false Value: false Focal Point: X: -1.26854742 Y: 1.47309268 Z: 0.652864814 Focal Shape Fixed Size: true Focal Shape Size: 0.0500000007 Invert Z Axis: false Name: Current View Near Clip Distance: 0.00999999978 Pitch: 0.829796612 Target Frame: <Fixed Frame> Value: Orbit (rviz) Yaw: 1.42941844 Saved: - Class: rviz/Orbit Distance: 79.2448425 Enable Stereo Rendering: Stereo Eye Separation: 0.0599999987 Stereo Focal Distance: 1 Swap Stereo Eyes: false Value: false Focal Point: X: -13.6964359 Y: -5.96537924 Z: -19.0381603 Focal Shape Fixed Size: true Focal Shape Size: 0.0500000007 Invert Z Axis: false Name: Orbit Near Clip Distance: 0.00999999978 Pitch: 1.1997968 Target Frame: <Fixed Frame> Value: Orbit (rviz) Yaw: 4.55750179 - Angle: -0.00499992538 Class: rviz/TopDownOrtho Enable Stereo Rendering: Stereo Eye Separation: 0.0599999987 Stereo Focal Distance: 1 Swap Stereo Eyes: false Value: false Invert Z Axis: false Name: TopDownOrtho Near Clip Distance: 0.00999999978 Scale: 17.4224415 Target Frame: <Fixed Frame> Value: TopDownOrtho (rviz) X: -8.52147961 Y: -7.69360256 - Angle: 0.0150001263 Class: rviz/TopDownOrtho Enable Stereo Rendering: Stereo Eye Separation: 0.0599999987 Stereo Focal Distance: 1 Swap Stereo Eyes: false Value: false Invert Z Axis: false Name: TopDownOrtho Near Clip Distance: 0.00999999978 Scale: 134.654221 Target Frame: <Fixed Frame> Value: TopDownOrtho (rviz) X: -15.3867512 Y: -4.14116335 - Angle: -0.0249942318 Class: rviz/TopDownOrtho Enable Stereo Rendering: Stereo Eye Separation: 0.0599999987 Stereo Focal Distance: 1 Swap Stereo Eyes: false Value: false Invert Z Axis: false Name: TopDownOrtho Near Clip Distance: 0.00999999978 Scale: 10.9894609 Target Frame: <Fixed Frame> Value: TopDownOrtho (rviz) X: -15.1368361 Y: -11.9202261 Window Geometry: Displays: collapsed: false Height: 1056 Hide Left Dock: false Hide Right Dock: false QMainWindow State: 000000ff00000000fd0000000400000000000001a2000003dafc020000000bfb000000100044006900730070006c00610079007301000000280000028a000000d700fffffffb0000000a0056006900650077007301000002b80000014a000000ad00fffffffb0000001200530065006c0065006300740069006f006e00000000000000009b0000006100fffffffb000000120056006900650077007300200054006f006f02000001df000002110000018500000122fb000000200054006f006f006c002000500072006f0070006500720074006900650073003203000002880000011d000002210000017afb0000001e0054006f006f006c002000500072006f007000650072007400690065007300000000e3000001c60000006100fffffffb0000002000730065006c0065006300740069006f006e00200062007500660066006500720200000138000000aa0000023a00000294fb00000014005700690064006500530074006500720065006f02000000e6000000d2000003ee0000030bfb0000000c004b0069006e0065006300740200000186000001060000030c00000261fb0000000a0049006d006100670065020000037e000000e30000016a00000089fb0000000a0049006d0061006700650200000593000001f50000016a000000ed000000010000010f000002c4fc0200000002fb0000001e0054006f006f006c002000500072006f00700065007200740069006500730100000041000000780000000000000000fb0000001200530065006c0065006300740069006f006e010000025a000000b20000000000000000000000020000073f000000ddfc0100000002fb0000000c00430061006d006500720061020000012e00000138000002d9000001a9fb0000000a00560069006500770073030000004e00000080000002e10000019700000003000005730000003efc0100000002fb0000000800540069006d00650000000000000005730000030000fffffffb0000000800540069006d0065010000000000000450000000000000000000000597000003da00000004000000040000000800000008fc0000000100000002000000010000000a0054006f006f006c00730100000000ffffffff0000000000000000 Selection: collapsed: false Time: collapsed: false Tool Properties: collapsed: false Views: collapsed: false Width: 1855 X: 65 Y: 24 修改这段代码,使其适于rviz2
07-27
### 修改 RVIZ 配置文件以兼容 ROS 2 Humble 的 `rviz2` 在 ROS 2 Humble 中,`rviz2` 的插件系统和配置结构与 ROS 1 的 `rviz` 有显著差异。为了使 RVIZ 配置文件适配 `rviz2`,需要调整插件类名、参数命名以及配置结构。 #### 1. 插件类名的命名空间变化 ROS 2 中的插件使用了新的命名空间结构。例如,在 ROS 1 中使用的 `rviz/Grid` 插件在 ROS 2 中应改为 `rviz_default_plugins/Grid`。类似地,其他插件如 `rviz/PointCloud2` 和 `rviz/Marker` 应分别改为 `rviz_default_plugins/PointCloud` 和 `rviz_default_plugins/Marker`。 ```yaml - Class: "rviz_default_plugins/Grid" Name: "Grid" Enabled: true Value: true ``` #### 2. 参数命名风格的调整 ROS 2 中的参数命名采用了更具一致性的风格,例如 `fixed_frame` 在 ROS 2 中应改为 `FixedFrame`,`background_color` 改为 `BackgroundColor`,这些参数通常位于 `Global Options` 下。 ```yaml Global Options: BackgroundColor: 255; 255; 127 FixedFrame: "world" ``` #### 3. 工具类插件的命名空间 工具类插件如 `rviz/MoveCamera` 和 `rviz/FocusCamera` 也应使用新的命名空间。例如: ```yaml Tools: - Class: "rviz_default_plugins/MoveCamera" - Class: "rviz_default_plugins/FocusCamera" ``` #### 4. 显示插件的参数适配 对于 `PointCloud2` 类型的显示插件,其参数如 `Color Transformer` 和 `Position Transformer` 在 ROS 2 中可能已更名或重构。例如,`AxisColor` 可能对应 `color_transformer: "Axis"`,而 `XYZ` 可能对应 `position_transformer: "XYZ"`。 ```yaml - Class: "rviz_default_plugins/PointCloud" Topic: "/demo_node/grid_map_vis" ColorTransformer: "Intensity" PositionTransformer: "XYZ" ``` #### 5. 视图配置的适配 视图配置中的 `Orbit` 和 `TopDownOrtho` 类型在 ROS 2 中保持存在,但其参数结构可能略有不同。例如,`Target Frame` 在 ROS 2 中可能为 `TargetFrame`,且部分参数如 `Distance` 和 `Pitch` 仍可直接使用。 ```yaml Views: Current: Class: "rviz_default_plugins/Orbit" Distance: 28.91 Pitch: 0.83 Yaw: 1.43 TargetFrame: "world" ``` #### 6. 状态栏与工具栏配置 状态栏和工具栏的配置在 ROS 2 中通常由 `Window Geometry` 控制,但其结构与 ROS 1 相比更为简化。部分配置如 `Toolbars` 和 `Selection` 可以保留,但需确保其结构符合 `rviz2` 的预期。 ```yaml Window Geometry: Width: 1855 Height: 1056 X: 65 Y: 24 ``` ### 示例:完整适配后的 RVIZ2 配置片段 ```yaml VisualizationManager: Tools: - Class: "rviz_default_plugins/MoveCamera" - Class: "rviz_default_plugins/FocusCamera" - Class: "rviz_default_plugins/Goal3DTool" Topic: "goal" Displays: - Class: "rviz_default_plugins/Grid" Name: "Grid" Enabled: true Value: true - Class: "rviz_default_plugins/Axes" Name: "Axes" Enabled: true Value: true - Class: "rviz_default_plugins/PointCloud" Topic: "/demo_node/grid_map_vis" ColorTransformer: "Intensity" PositionTransformer: "XYZ" Enabled: true Value: true - Class: "rviz_default_plugins/Marker" Topic: "/demo_node/grid_path_vis" Name: "gridPath" Enabled: true Value: true Global Options: BackgroundColor: 255; 255; 127 FixedFrame: "world" ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值