首页 > 其他 > 详细

Leetcode 226 Invert Binary Tree

时间:2015-06-20 00:20:00      阅读:258      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!