题解 | #二叉树的镜像#
二叉树的镜像
http://www.nowcoder.com/practice/a9d0ecbacef9410ca97463e4a5c83be7
package org.example.test; public class MirrorTest { static public class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode(int val) { this.val = val; } } /** * 左右互相交换即可 * * @param pRoot * @return */ public TreeNode Mirror(TreeNode pRoot) { // write code here if (pRoot == null) { return null; } TreeNode left = pRoot.left; TreeNode right = pRoot.right; pRoot.left = right; pRoot.right = left; Mirror(left); Mirror(right); return pRoot; } }
算法 文章被收录于专栏
数据结构和算法