lintcode翻转二叉树

来源:互联网 发布:淘宝上的pa改异形 编辑:程序博客网 时间:2024/05/20 01:09

翻转二叉树 

Accepted

总耗时: 254 ms
100% 数据通过测试.
还没解决的相关题目
    389. 判断数独是否合法248. 统计比给定整数小的数的个数249. 统计前面比自己小的数的个数131. 大楼轮廓370. 将表达式转换为逆波兰表达式
太牛了,把AC的喜悦分享给你的朋友吧!

解析:这道题的树并不是二叉查找树,所以对每个结点的左右结点交换即可。

/** * Definition of TreeNode: * class TreeNode { * public: *     int val; *     TreeNode *left, *right; *     TreeNode(int val) { *         this->val = val; *         this->left = this->right = NULL; *     } * } */class Solution {public:    /*     * @param root: a TreeNode, the root of the binary tree     * @return: nothing     */    void invertBinaryTree(TreeNode * root) {        // write your code here        if(root==NULL)        return ;        invertBinaryTree(root->left);//后续遍历,先对左右结点操作        invertBinaryTree(root->right);        TreeNode *tmp;        tmp=root->left;        root->left=root->right;        root->right=tmp;    }};