面试题19:二叉树的镜像

来源:互联网 发布:简易电路图绘制软件 编辑:程序博客网 时间:2024/06/05 03:43


面试题19:二叉树的镜像

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。 
输入描述:
二叉树的镜像定义:源二叉树         8       /  \      6   10     / \  / \    5  7 9 11    镜像二叉树        8       /  \      10   6     / \  / \    11 9 7  5
代码:

package offer;/** * 面试题19:二叉树的镜像 * 操作给定的二叉树,将其变换为源二叉树的镜像。 */public class _19_tree_mirror {    public static void main(String[] args){        TreeNode19 t1 = new TreeNode19(1);        t1.left=new TreeNode19(2);        t1.right=new TreeNode19(3);        t1.left.left=new TreeNode19(4);        t1.left.right=new TreeNode19(5);        Solution19 solution19 = new Solution19();        solution19.Mirror(t1);        solution19.preOrderTraversal(t1);    }}class Solution19 {    public void Mirror(TreeNode19 root) {        if(root==null){            return;        }        if(root.left==null&&root.right==null){            return;        }        TreeNode19 tmp=root.left;        root.left=root.right;        root.right=tmp;        if(root.left!=null){            Mirror(root.left);        }        if(root.right!=null){            Mirror(root.right);        }    }    /**     * 先序遍历     * @param node 二叉树的根节点     */    public void preOrderTraversal(TreeNode19 node){        if(node!=null){            System.out.print(node.val+"、");            preOrderTraversal(node.left);            preOrderTraversal(node.right);        }    }}class TreeNode19 {    int val = 0;    TreeNode19 left = null;    TreeNode19 right = null;    public TreeNode19(int val) {        this.val = val;    }}


0 0
原创粉丝点击