LeetCode题解–226. Invert Binary Tree

来源:互联网 发布:sql数据库置疑怎么解决 编辑:程序博客网 时间:2024/06/08 18:21

链接

LeetCode题目:https://leetcode.com/problems/invert-binary-tree/
难度:Easy

题目

Invert a binary tree.

分析

这题比较简单,利用分治的想法就能翻转二叉树。

代码

class Solution {public:    TreeNode* invertTree(TreeNode* root) {        if(root == nullptr) return root;        swap(root->left, root->right);        invertTree(root->left);        invertTree(root->right);        return root;    }};
原创粉丝点击