美文网首页
Invert Binary Tree - 反转二叉树

Invert Binary Tree - 反转二叉树

作者: 郑明明 | 来源:发表于2016-11-03 12:18 被阅读175次
  • 题目:对于二叉树中的每个节点,将左右子节点互换
  • 分析:这道题其实不难,如果对递归的思想根深蒂固的话,那么可以很快地解除题目
  • 代码:
TreeNode* invertTree(TreeNode* root) {
        if(root == NULL) {
            return NULL;
        }
        if(root->left == NULL && root->right == NULL) {
            return root;
        }
        TreeNode *left = invertTree(root->right);
        TreeNode *right = invertTree(root->left);
        root->left = left;
        root->right = right;
        return root;
    }

相关文章

网友评论

      本文标题:Invert Binary Tree - 反转二叉树

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