标签:roo 左右 github 返回 bin str tree c++ 解法
https://leetcode-cn.com/problems/invert-binary-tree/
// Problem: LeetCode 226
// URL: https://leetcode-cn.com/problems/invert-binary-tree/
// Tags: Tree Recursion
// Difficulty: Easy
#include <iostream>
using namespace std;
struct TreeNode{
TreeNode* left;
TreeNode* right;
int val;
TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};
class Solution{
public:
TreeNode* invertTree(TreeNode* root) {
// 递归出口,空结点直接返回
if(root==nullptr)
return nullptr;
// 该结点交换左右子树
TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
// 递归翻转左右子树
invertTree(root->left);
invertTree(root->right);
return root;
}
};
作者:@臭咸鱼
转载请注明出处:https://www.cnblogs.com/chouxianyu/
欢迎讨论和交流!
标签:roo 左右 github 返回 bin str tree c++ 解法
原文地址:https://www.cnblogs.com/chouxianyu/p/13377535.html