二叉树的镜像

题目描述

输入一个二叉树,输出其镜像。

class Solution {
public:
	void Mirror(TreeNode *pRoot) {
		if (pRoot==NULL || (pRoot->left==NULL&&pRoot->right==NULL)) {
            return;
		}
		TreeNode *t = pRoot->left;
		pRoot->left = pRoot->right;
		pRoot->right = t;
		if (pRoot->left != NULL) {
            Mirror(pRoot->left);
		}
		if (pRoot->right != NULL) {
            Mirror(pRoot->right);
		}
	}
};
 
public class Solution {
    public void Mirror(TreeNode root) {
    	if (root==null || (root.left==null && root.right==null)) {
    		return;
    	}
    	TreeNode t = root.left;
    	root.left = root.right;
    	root.right = t;
    	if (root.left != null) {
    		Mirror(root.left);
    	}
    	if (root.right != null) {
    		Mirror(root.right);
    	}
    }
}
 

你可能感兴趣的:(二叉树)