Skip to content

leetcode 226. 翻转二叉树

我的解题

#include <bits/stdc++.h>
using namespace std;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

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 (!root)
        {
            return root;
        }
        TreeNode *left = invertTree(root->left);
        TreeNode *right = invertTree(root->right);
        root->right = left;
        root->left = right;
        return root;
    }
};

labuladong 东哥手把手带你套框架刷通二叉树|第一期

在其中,使用了pre-order的方式来解决的。