Invert Binary (recursive and iterative)

两种做饭 递归相对好做

1. recursive

class Solution {
public:
    void swap(TreeNode** left, TreeNode** right)
    {
        TreeNode* temp = (*left);
        *left = *right;
        *right = temp;
    }
    TreeNode* invertTree(TreeNode* root) {
        
        if (root == nullptr)
            return root;
        invertTree(root->left);
        invertTree(root->right);
        swap ((&root->left), (&root->right));
        return root;
        
        
    }
};
其实核心思想还是 1.左边到底 2.借助程序的stack 自动跳回到上一根结点 然后再左边到底

2. Iterative

你可能感兴趣的:(Invert Binary (recursive and iterative))