翻转二叉树

来源:互联网 发布:提醒事项软件 编辑:程序博客网 时间:2024/05/29 23:23

翻转一棵二叉树

样例
  1         1 / \       / \2   3  => 3   2   /       \  4         4
解题思路:dfs进行遍历,并且建立一个临时指针,每次对左右结点进行地址调换即可。
class Solution {public:    /**     * @param root: a TreeNode, the root of the binary tree     * @return: nothing     */     TreeNode *tem;    void invertBinaryTree(TreeNode *root) {        // write your code here        if(root==NULL)return;    tem=root->right;    root->right=root->left;    root->left=tem;    invertBinaryTree(root->left);    invertBinaryTree(root->right);    }


0 0
原创粉丝点击