码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 226 Invert Binary Tree

时间:2015-06-20 00:22:52      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:二叉树   交换子树   

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;
}

Leetcode 226 Invert Binary Tree

标签:二叉树   交换子树   

原文地址:http://blog.csdn.net/jeanphorn/article/details/46566909

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!