美文网首页
LeetCode 226.翻转二叉树

LeetCode 226.翻转二叉树

作者: 饼干不干 | 来源:发表于2019-06-22 22:03 被阅读0次

翻转一棵二叉树。

示例:
输入:

     4
   /   \
  2     7
 / \   / \
1   3 6   9
输出:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

C++

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

相关文章

网友评论

      本文标题:LeetCode 226.翻转二叉树

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