LeetCode-Mirror of Binary Tree

Title description

Problem-solving ideas

  • What needs to be clearly pointed out is the sorted root object we return
  • We first judge whether the left and right child nodes of the root parameter are empty. As long as they are empty, the sorting is done.
  • If it is not empty, the exchange method is used to exchange the left and right pointers.
  • Finally, recursion is used to continue putting the left and right child nodes into the function.

Implementation code

var mirrorTree = function (root) {
    
    
    fun(root);
    return root;

};
var fun = function (root) {
    
    


    if (root !== null) {
    
    
        var temp;
        temp = root.left;
        root.left = root.right;
        root.right = temp;
        mirrorTree(root.left);
        mirrorTree(root.right);
    }

};

Guess you like

Origin blog.csdn.net/sinat_41696687/article/details/114866814