xqn2017

导航

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

统计

226. Invert Binary Tree

原文题目:

https://leetcode.com/problems/invert-binary-tree/description/

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     
};
class Solution
{
public:
    TreeNode* invertTree(TreeNode* root)
    {
        if (NULL == root)
        {
            return NULL;
        }
        /*交换左右子树*/
        TreeNode* temp = root->left;
        root->left = root->right;
        root->right = temp;
 
        /*递归左右子树*/
        invertTree(root->left);
        invertTree(root->right);
 
        return root;
 
    }
};

  

posted on   xqn2017  阅读(134)  评论(0编辑  收藏  举报

点击右上角即可分享
微信分享提示