美文网首页
【LeetCode】 翻转二叉树

【LeetCode】 翻转二叉树

作者: MyyyZzz | 来源:发表于2019-04-08 18:18 被阅读0次

题目描述:

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

解题思路:

递归;
第一步:终止条件:root==NULL,返回NULL;
第二步:返回值:返回交换左右子树后的根结点root;
第三步:本级应该做的事:交换根结点root的左右子树

代码:

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(!root)
            return NULL;
        if(root->left || root->right)
        {
            TreeNode* temp = root->right;
            root->right = root->left;
            root->left = temp;
        }
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

相关文章

网友评论

      本文标题:【LeetCode】 翻转二叉树

      本文链接:https://www.haomeiwen.com/subject/obbpiqtx.html