树三:二叉树的镜像
/**
* 题目:二叉树的镜像
* 描述:二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5
* 解决方案: ①注意特殊输入的判断;②交换根结点的左右孩子结点③递归访问所有的结点(每个结点的左右孩子交换)
* */
public class Three { public static void one(TreeNode tree) { if(tree == null ||tree.left == null || tree.right == null) { return ; } TreeNode treeNode = null; treeNode = tree.left; tree.left = tree.right; tree.right = treeNode; one(tree.left); one(tree.right); } static class TreeNode{ int var; TreeNode left; TreeNode right; } }
天助自助者