翻转二叉树

翻转一棵二叉树

样例
  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);
    }


你可能感兴趣的:(数据结构—二叉树)