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

反转二叉树----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 root) {
                if(root==null)
                    return root;
        TreeNode temp=root.left;
        root.left=invertNode(root.right);
        root.right=invertNode(temp);
        return root;
    }  

非递归

public TreeNode invertNode(TreeNode root) {
        if(root==null)
            return null;
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        queue.add(root);
        while(queue!=null){
            TreeNode current=queue.poll();
            TreeNode temp=current.left;
            current.left=current.right;
            current.right=temp;
            if(current.left!=null)
                queue.add(current.left);
            if(current.right!=null)
                queue.add(current.right);
        }
        return root;
    }  

原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/10727148.html

时间: 2025-01-17 13:50:44

二叉树的镜像(反转二叉树)的相关文章

剑指Offer 18. 二叉树的镜像 (二叉树)

题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / 6 10 / \ / 5 7 9 11 镜像二叉树 8 / 10 6 / \ / 11 9 7 5 题目地址 https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-i

C++算法之 判断是否为平衡二叉树 求二叉树的镜像

1:判断是否为平衡二叉树: //方法1: int TreeDepth(BTree* pRoot) { if (pRoot == NULL) return 0; int nLeftDepth = TreeDepth(pRoot->m_pLeft); int nRightDepth = TreeDepth(pRoot->m_pRight); return (nLeftDepth > nRightDepth)? (nLeftDepth+1):(nRightDepth+1); } bool Is

刷题18 二叉树的镜像

描述:  操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的镜像示例: 源二叉树 8 / 6 10 / \ / 5 7 9 11 镜像二叉树 8 / 10 6 / \ / 11 9 7 5 很容易的想到了递归: 1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) {

18 二叉树的镜像

题目描述:操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的镜像定义:源二叉树 8 / 6 10 / \ / 5 7 9 11 镜像二叉树 8 / 10 6 / \ / 11 9 7 5 1 /** 2 public class TreeNode { 3 int val = 0; 4 TreeNode left = null; 5 TreeNode right = null; 6 public TreeNode(int val) { 7 this.val = val; 8 } 9 } 10

反转二叉树

输入一个二叉树,输出其镜像. 如下图,即交换所有节点的左右子树. 这里提供两种思路:使用递归和不使用递归. 使用的二叉树定义如下: 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.Sca

二叉树的镜像

什么是二叉树的镜像呢? 我们可以自己画一颗二叉树.然后根据照镜子画出它的镜像. 如: 我们不能一次得到二叉树的镜像,要想得到一颗二叉树的镜像,有以下几个步骤: (1)先交换根的左子树和右子树 (2)交换6的左子树和右子树                      (3)交换10的左子树和右子树 得出以上规律后,就可以写代码喽: class BinaryTreeNode { public: BinaryTreeNode(const T& data) :_data(data) ,_left(NULL

【剑指offer】二叉树的镜像

转载请注明出处:http://blog.csdn.net/ns_code/article/details/25915971 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000,n代表将要输入的二叉树节点的个数(节点从1开始编号).接下来一行有n个数字,代表第i个二叉树节点的元素的值.接下来有n行,每行有一个字母Ci.Ci='d'表示第i个节点有两子孩子,紧接着是左孩子编号和右孩子编号.C

【剑指offer】十二,二叉树的镜像

题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 分析:镜像的递归定义就是将原有二叉树中节点的左右子树对调.代码如下: 1 /** 2 public class TreeNode { 3 int val = 0; 4 TreeNode left = null; 5 TreeNode right = null; 6 7 public TreeNode(int val) { 8 this.val = val; 9 10 } 11 12 } 13 */ 14 public class Solut

剑指Offer 二叉树的镜像

题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / 6 10 / \ / 5 7 9 11 镜像二叉树 8 / 10 6 / \ / 11 9 7 5 思路: 直接一个中间指针,递归,交换左右节点,节点为叶子节点的时候返回. AC代码: 1 class Solution { 2 public: 3 void Mirror(TreeNode *pRoot) { 4 if(pRoot==NULL) 5 return ; 6 7 TreeNode *