Leetcode 226 Invert Binary Tree

1.问题描述

  交换二叉树的左右子树。
  

2. 方法思路

  直接递归交换左右子树即可,c代码如下:
  

/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
struct TreeNode* invertTree(struct TreeNode* root) {
    if(root == NULL) return NULL;

    struct TreeNode *tmp = root->left;
    root->left = root->right;
    root->right = tmp;

    invertTree(root->left);
    invertTree(root->right);

    return root;
}

你可能感兴趣的:(二叉树,交换子树)