leetCode-Record icon indicating copy to clipboard operation
leetCode-Record copied to clipboard

面试题27. 二叉树的镜像

Open fireairforce opened this issue 5 years ago • 0 comments

递归即可:

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var mirrorTree = function(root) {
   if(!root) {
       return null;
   }
   let temp = root.left;
   root.left = mirrorTree(root.right);
   root.right = mirrorTree(temp);
   return root; 
};

fireairforce avatar Feb 21 '20 15:02 fireairforce