// invert binary tree : (right node >> left node && left node >> right node )
const invertTree =(root)=>{
if(root===null) return root;
let queue =[root] ;
while(queue.length>0){
let node =queue.shift() ;
if(node !=null){
let hold = node.left ;
node.left =node.right;
node.right =hold ;
if(node.left) queue.push(node.left) ;
if(node.right) queue.push(node.right) ;
}
}
return root;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL){
return 0;
}
invertTree(root->left);
invertTree(root->right);
swap(root->left,root->right);
return root;
}
};
// invert binary tree using recursive function
function invertTree(node) {
if (node && node.left) {
let left = node.left;
node.left = node.right;
node.right = left;
invertTree(node.right);
invertTree(node.left);
}
return node;
}
// invert binary tree : (right node >> left node && left node >> right node )
const invertTree =(root)=>{
if(root===null) return root;
let queue =[root] ;
while(queue.length>0){
let node =queue.shift() ;
if(node !=null){
let hold = node.left ;
node.left =node.right;
node.right =hold ;
if(node.left) queue.push(node.left) ;
if(node.right) queue.push(node.right) ;
}
}
return root;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL){
return 0;
}
invertTree(root->left);
invertTree(root->right);
swap(root->left,root->right);
return root;
}
};
// invert binary tree using recursive function
function invertTree(node) {
if (node && node.left) {
let left = node.left;
node.left = node.right;
node.right = left;
invertTree(node.right);
invertTree(node.left);
}
return node;
}