Skip to content

Latest commit

 

History

History
36 lines (29 loc) · 675 Bytes

invert-binary-tree.MD

File metadata and controls

36 lines (29 loc) · 675 Bytes

Invert Binary Tree @ LeetCode

https://leetcode.com/problems/invert-binary-tree/


1st Approach

 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr) {
            return root;
        }
        
        TreeNode *temp = invertTree(root->left);
        root->left = invertTree(root->right);
        root->right = temp;
        
        return root;
    }
};

2nd Approach

  • Iterative solution: Use queue