175.翻转二叉树

来源:互联网 发布:雷阿伦生涯总数据 编辑:程序博客网 时间:2024/06/01 19:26

题目:翻转一棵二叉树


样例:

  1         1 / \       / \2   3  => 3   2   /       \  4         4

代码:

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;        else         {         TreeNode *t=root->left;        root->left=root->right;        root->right=t;        invertBinaryTree(root->left);        invertBinaryTree(root->right);        }    }};


感想:从根开始,左右子树调换,然后利用递归的思想,遍历左右子树,即可完成翻转。

0 0
原创粉丝点击