Search
 
SCRIPT & CODE EXAMPLE
 

CPP

invert tree

// 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;

 }
Comment

invert a binary tree

/**
 * 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;
    }
};
Comment

invert binary tree

// 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;
}
Comment

invert tree

// 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;

 }
Comment

invert a binary tree

/**
 * 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;
    }
};
Comment

invert binary tree

// 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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: abs in c++ used for 
Cpp :: split text c++ 
Cpp :: A Program to check if strings are rotations of each other or not 
Cpp :: unique element in array in c 
Cpp :: flutter text direction auto 
Cpp :: memset c++ 
Cpp :: c++ last element of vector 
Cpp :: convert uppercase to lowercase 
Cpp :: Write a C++ program using constructor 
Cpp :: auto in cpp 
Cpp :: c++ shared pointer operator bool 
Cpp :: summation of numbers using function 
Cpp :: InstallUtil.exe ConsoleApp 
Cpp :: pointer to value of others file cpp 
Cpp :: graph colouring 
Cpp :: how to read rotary encoder c++ 
Cpp :: time_t c++ stack overflow 
Cpp :: find n unique integers sum up to zero 
Cpp :: apertura file in c++ 
Cpp :: convert c++ to c online 
Cpp :: long, long long 32 bit or 8 bit in c++ 
Cpp :: overwrite windows mbr c++ 
Cpp :: Missing GL version 
Cpp :: convert into acsii c++ 
Cpp :: Equalize problem codeforces 
Cpp :: rgb(100,100,100,0.5) validation c++ 
Cpp :: how to get max grade c++ 
Cpp :: 400 watt hour per kg 
Cpp :: txt auslesen c++ 
Cpp :: linked 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =