Prove safety offer [18] - a binary image

Title Description

Operation of a given binary tree, the binary tree is converted into a source image.

Enter a description:

二叉树的镜像定义:源二叉树 
          8
         /  \
        6   10
       / \  / \
      5  7 9 11
      镜像二叉树
          8
         /  \
        10   6
       / \  / \
      11 9 7  5

The purpose of this question is actually very simple idea, as long as we recursively traverse the source binary tree, then left and right subtrees switching node down through to the end of the process Consider the following illustration:

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function Mirror(root)
{
    function change(head){
        if(!head){return false;}
        let temp = head.left;
        head.left = head.right;
        head.right = temp;
        change(head.left);
        change(head.right);
    }
    change(root);
    return root;
}

Guess you like

Origin www.cnblogs.com/Jacob98/p/12469236.html
Recommended