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 *)malloc(sizeof(Tree));
        (*T)->val = val;
        CreateBiTree(&(*T)->left);
        CreateBiTree(&(*T)->right);
    }
}

void ExchangeLeftRight(Tree **root)
{
    Tree * node = (*root)->right;
    (*root)->right = (*root)->left;
    (*root)->left = node;
}

void RecursiveReversal(Tree *root) //反转二叉树
{
    if (root != NULL)
    {
        ExchangeLeftRight(&root);
        RecursiveReversal(root->left);
        RecursiveReversal(root->right);
    }
}

void Print(Tree*root)
{
    if (root != NULL)
    {
        Print(root->left);
        printf("%d ", root->val);
        Print(root->right);
    }
}

int main()
{
    Tree* root;

    CreateBiTree(&root);
    Print(root);
    printf("\n");
    RecursiveReversal(root);
    Print(root);

    return 0;
}

  没什么好解释的。

原文地址:https://www.cnblogs.com/darkchii/p/8575240.html

时间: 2024-08-04 04:51:26

C/C++ - 反转二叉树的相关文章

反转二叉树

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

【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

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