Binary tree mirror transformation

Binary tree mirror transformation

Problem description: Operate the given binary tree and transform it into a mirror image of the source binary tree.

Code:

package hgy.java.arithmetic;

public class BinaryTreeMirror {
	public class TreeNode {
	    int val = 0;
	    TreeNode left = null;
	    TreeNode right = null;
	    public TreeNode(int val) {
	        this.val = val;
	    }
	}
	
	public void mirror(TreeNode root) {
		if(root == null)
			return ;
		change(root);
		mirror(root.left);
		mirror(root.right);	
	}
	
	//交换结点左右子树
	public void change(TreeNode root){
		TreeNode node = root.left;
		root.left = root.right;
		root.right = node;
	}
}

Published 6 original articles · praised 0 · visits 21

Guess you like

Origin blog.csdn.net/qq_35419705/article/details/105693770