leetcode226 翻转二叉树(Easy)

Posted 人生苦短,及时刷题

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode226 翻转二叉树(Easy)相关的知识,希望对你有一定的参考价值。

题目来源:leetcode226 翻转二叉树

题目描述:

翻转一棵二叉树。

解题思路

递归

/**
 * Definition for a binary tree node.
 * 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==NULL) return NULL;
        TreeNode* temp=root->left;
        root->left=root->right;
        root->right=temp;
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

以上是关于leetcode226 翻转二叉树(Easy)的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode | 226. 翻转二叉树

LeetCode226. 翻转二叉树

LeetCode #226 翻转二叉树

LeetCode Java刷题笔记—226. 翻转二叉树

Leetcode226. 翻转二叉树(JAVA递归)

Leetcode刷题100天—226. 翻转二叉树(二叉树)—day03