反转二叉树

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

如下图,即交换所有节点的左右子树。

这里提供两种思路:使用递归和不使用递归。

使用的二叉树定义如下:

public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }
}

解决方法:

import java.util.LinkedList;
import java.util.Scanner;

/*
 * 题目描述:输入一个二叉树,输出其镜像。
 * */
public class Solution {
	Scanner scanner = new Scanner(System.in);

	// 建立二叉树
	public TreeNode createTree(TreeNode root) {
		String val;
		val = scanner.next(); // next方法每次取到一个间隔符前面的数据
		if(val.equals("#")) {
			return null;
		}
		root = new TreeNode(Integer.parseInt(val));  System.out.println("输入的数据为:" + val);
		root.left = createTree(root.left);
		root.right = createTree(root.right);
		return root;
	}
	// 得到二叉树的镜像  —— 递归的方式
    public void Mirror(TreeNode root) {
    	if(root == null) {
    		return;
    	}
    	if((root.left == null) && (root.right == null)) {
    		return;
    	}
    	TreeNode temp = root.left;
    	root.left = root.right;
    	root.right = temp;
    	Mirror(root.left);
    	Mirror(root.right);
    }
    // 得到二叉树的镜像 —— 不使用递归
    public void MirrorNotRecursive(TreeNode root) {
    	java.util.LinkedList<TreeNode> stack = new java.util.LinkedList<TreeNode>();
    	TreeNode temp = null;
    	if(root == null) {
    		return;
    	}
    	stack.add(root);
    	while(stack.size() != 0) {
    		TreeNode node = stack.removeFirst();
    		temp = node.left;
    		node.left = node.right;
    		node.right = temp;
    		if(node.right != null) {
    			stack.add(node.right);
    		}
    		if(node.left != null) {
    			stack.add(node.left);
    		}
    	}
    }

    // 层次遍历二叉树
    public void levelTraverse(TreeNode root) {
    	if (root == null) {
    		return;
    	}
    	LinkedList<TreeNode> list = new LinkedList<TreeNode>();
    	list.add(root);
    	while (list.size() != 0) {
    		TreeNode node = list.removeFirst(); // list.removeFirst() 该方法LinkedList才有
    		System.out.print(node.val + " ");
    		if(node.left != null) {
    			list.add(node.left);
    		}
    		if(node.right != null) {
    			list.add(node.right);
    		}
    	}
    }

    public static void main(String[] args) {
    	Solution solution = new Solution();
    	TreeNode root = null;
    	root = solution.createTree(root);
    	System.out.println("原二叉树的层次遍历");
    	solution.levelTraverse(root);
    	solution.Mirror(root);
    	System.out.println("\n输出该二叉树的镜像");
    	solution.levelTraverse(root);
    	solution.MirrorNotRecursive(root);
    	System.out.println("\n输出该二叉树的镜像(非递归方式)");
    	solution.levelTraverse(root);
    }
}

/*
 * 测试数据:
 * 1 2 3 # 4 # # 5 6 # # # 7 8 # # 9 10 # # 11 # #  (说明:其中#说明左右子树为空)
 * 用先序遍历来建立树后,层次遍历结果为: 1 2 7 3 5 8 9 4 6 10 11
 * 反转二叉树之后:1 7 2 9 8 5 3 11 10 6 4
 */
时间: 2024-12-08 11:49:47

反转二叉树的相关文章

【LeetCode-面试算法经典-Java实现】【226-Invert Binary Tree(反转二叉树)】

[226-Invert Binary Tree(反转二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 题目大意 将一棵二叉树进行翻转. 解题思路 对每一个结点,将它的左右子树进行交换,再对它的左右子结点进行同样的操作. 代码实现 树结点类 pub

反转二叉树,即交换所有结点的左右子树,但不能使用递归方法。

反转二叉树,即交换所有结点的左右子树,但不能使用递归方法. 解析:既然不能使用递归那么可以使用栈,代码如下: #include <iostream> #include<stack> #include<assert.h> #include <tchar.h> #include <queue> using namespace std; typedef struct BinaryTreeNode { int m_nValue; BinaryTreeNo

C/C++ - 反转二叉树

由上而下递归反转代码: #include <stdio.h> #include <stdlib.h> typedef struct Tree { int val; struct Tree* left; struct Tree* right; }Tree; void CreateBiTree(Tree**T) { int val; scanf("%d", &val); if(val == -1) *T = NULL; else { *T = (Tree *

leetCode题解之反转二叉树

1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 1 TreeNode* invertTree(TreeNode* root) { 2 3 if(root == NULL ) 4 return NULL; 5 6 7 TreeNode* temp; 8 temp = root->left; 9 root->left = invertTree(root->right); 10 root->right = invertTree(temp)

二叉树的镜像(反转二叉树)

反转二叉树----java实现 描述 实现二叉树的反转 示例: 原二叉树: 4 / 2 7 / \ / 1 3 6 9 反转后的二叉树: 4 / 7 2 / \ / 9 6 3 1 解析 递归 1.判断根是否为空,根为空直接返回根:否则继续:2.递归反转根的左右子树 非递归 1.判断根是否为空,根为空直接返回根:否则继续:2.交换根节点的左右子节点:3. 交换第二层结点的左右子树:4 重复下去,最后一个结点. 代码 递归 public TreeNode invertNode(TreeNode r

LeetCode 226. Invert Binary Tree (反转二叉树)

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

反转二叉树 打印二叉树

代码: package com.qhong; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class Main { public static void main(String[] args) { TreeNode root =new TreeNode(0); TreeNode left=new TreeNode(1); TreeNode right=new Tre

LeetCode Invert Binary Tree 反转二叉树

思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solutio