Topic describes

Their thinking

  • To be clear, we return the sorted root object
  • We first check whether the left and right child nodes of the root parameter are empty. If they are empty, the sorting is done.
  • If it is not null, the left and right Pointers are swapped using the swap method.
  • Finally, using recursion, the left and right child nodes are continued into the function.

The implementation code

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

};
var fun = function (root) {


    if(root ! = =null) {
        vartemp; temp = root.left; root.left = root.right; root.right = temp; mirrorTree(root.left); mirrorTree(root.right); }};Copy the code