基础有待加强啊,由该题引发出来一些问题,现在来总结下。

首先是二叉树的结构:

 struct TreeNode {
EleType val;
TreeNode *left;
TreeNode *right;
};

然后是二叉树,先序遍历的构建方法,由于只有扩展后的二叉树可以做到一个遍历序列确定一颗二叉树,比如图所示前序遍历序列(根左右)就为12#4##3##。

二叉树构建的代码,因为要对传递的值进行改变,所以不能值传递,所以注意这里的参数为指向TreeNode类型的指针的一个引用,

这是因为如果直接传递指针变量,给该函数的形参初始化之后,该形参在退出该函数就自动回收啦。

int CreateBiTree(TreeNode* &T)
{
char data;
//按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
scanf("%c",&data);
if(data == '#'){
T = NULL;
}
else{
T = (TreeNode*)malloc(sizeof(TreeNode));
T->val = data;
CreateBiTree(T->left);
CreateBiTree(T->right);
}
return ;
}

该题的思路:主要有递归和栈来实现两种方法。中心对称即左子树中某个节点的左孩子=对应的右子树的节点的右孩子,该节点的右孩子=对应结点的左孩子。

  

代码:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
typedef int EleType; struct TreeNode {
EleType val;
TreeNode *left;
TreeNode *right;
}; class Solution {
public:
bool check(TreeNode *leftNode, TreeNode *rightNode)
{
if (leftNode == NULL && rightNode == NULL)
return true; if (leftNode == NULL || rightNode == NULL)
return false; return leftNode->val == rightNode->val && check(leftNode->left, rightNode->right) &&
check(leftNode->right, rightNode->left);
} bool isSymmetric(TreeNode *root) {
if (root == NULL)
return true;
return check(root->left, root->right);
}
}; //按先序序列创建二叉树
int CreateBiTree(TreeNode* &T){
int data;
//按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
cin>>data;
if(data == -1){
T = NULL;
}
else{
T = (TreeNode*)malloc(sizeof(TreeNode));
//生成根结点
T->val = data;
//构造左子树
CreateBiTree(T->left);
//构造右子树
CreateBiTree(T->right);
}
return ;
} int main()
{
freopen("C:\\Users\\Administrator\\Desktop\\test.txt","r",stdin);
TreeNode* root=NULL;
CreateBiTree(root);
Solution so;
cout<<so.isSymmetric(root)<<endl;
return ;
}

ps:递归的终止条件:左节点和右节点都为空,则true;

左节点和右节点中只有一个不为空,返回false(因为上面的判断保证了肯定有一个不为空)

Symmetric Tree(DFS,二叉树的构建以及测试代码)的更多相关文章

  1. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  2. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. [Leetcode] Symmetric tree 对称二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  4. 【LeetCode】Symmetric Tree(对称二叉树)

    这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...

  5. 606. Construct String from Binary Tree 从二叉树中构建字符串

    [抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...

  6. 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...

  7. 二叉树BinaryTree构建测试(无序)

    此测试仅用于二叉树基本的性质测试,不包含插入.删除测试(此类一般属于有序树基本操作). //二叉树树类 public class BinaryTree { public TreeNode root; ...

  8. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  9. ※数据结构※→☆非线性结构(tree)☆============二叉树 顺序存储结构(tree binary sequence)(十九)

    二叉树 在计算机科学中,二叉树是每个结点最多有两个子树的有序树.通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree).二叉树常被用作二叉查找树和二叉堆或是 ...

随机推荐

  1. linux中vim永久显示行号、开启语法高亮

    vim ~/.vimrc 进入insert模式,在最后加二行 syntax on set nu! 保存收工. 设置用视图模式的缩进为4个空格 set smartindent set tabstop=4 ...

  2. apicloud入门学习笔记1:简单介绍

    官网地址:https://www.apicloud.com/ 新手开发指南:https://docs.apicloud.com/APICloud/junior-develop-guide 开发语言:H ...

  3. JAVA基础篇—多态

    class ColaEmployee父类 package com.cola; public class ColaEmployee { private String name; private int ...

  4. proc_info_list

    内核中每种处理器架构抽象为一个proc_info_list结构体,在arch/arm/include/asm/procinfo.h中定义, struct proc_info_list { unsign ...

  5. Jack Straws POJ - 1127 (几何计算)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5428   Accepted: 2461 Descr ...

  6. hdu-2544 最短路(最短路)

    Time limit1000 ms Memory limit32768 kB   在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到 ...

  7. CodeForces 703A Mishka and trip

    Description Little Mishka is a great traveller and she visited many countries. After thinking about ...

  8. Erasing and Winning UVA - 11491 贪心

    题目:题目链接 思路:不难发现,要使整体尽量大,应先满足高位尽量大,按这个思路优先满足高位即可 AC代码: #include <iostream> #include <cstdio& ...

  9. Linux学习-编译前的任务:认识核心与取得核心原始码

    什么是核心 (Kernel) Kernel 其实核心就是系统上面的一个文件而已, 这个文件包含了驱动主机各项硬 件的侦测程序与驱动模块. 核心文件通常被放置成 /boot/vmlinuz-xxx ,不 ...

  10. bash脚本编写基础

    bash脚本编程     命令的堆砌     脚本程序:解释器解析执行     shell:交互式接口,编程环境         shell:能够提供一些内部命令,并且能通过PATH环境变量找到外部命 ...