zoukankan      html  css  js  c++  java
  • 剑指offer 二叉树的镜像

    题目:操作给定的二叉树,将其变换为源二叉树的镜像。

    代码:

     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) {
     8     }
     9 };*/
    10 class Solution {
    11 public:
    12     void Mirror(TreeNode *pRoot) {
    13         if(pRoot == NULL) return ;
    14         else{
    15             TreeNode *temp = new TreeNode(NULL);
    16             temp = pRoot->left;
    17             pRoot->left = pRoot->right;
    18             pRoot->right = temp;
    19             Mirror(pRoot->left);
    20             Mirror(pRoot->right);
    21         }
    22     }
    23 };

    我的笔记:将每个根节点的左右子节点交换,并递归遍历每个结点。

  • 相关阅读:
    Linux基础
    杂谈
    MySQL基础
    Effective Java-第4章
    Effective Java-第三章
    Effective Java-第二章
    mybatis
    mapper.xml文件
    Mybatis
    mybatis-config.xml文件详解
  • 原文地址:https://www.cnblogs.com/john1015/p/12950685.html
Copyright © 2011-2022 走看看