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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following [1,2,2,null,3,null,3] is not:

    1
/ \
2 2
\ \
3 3

Note:
Bonus points if you could solve it both recursively and iteratively.

--------------------------------------------------------------------------------------------------------------------------

symmetric是对称的,此题用DFS会比较简单的,关键是要找出合适的递归方法。

C++代码:

官方题解:https://leetcode.com/problems/symmetric-tree/solution/

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return Recur(root,root);
}
bool Recur(TreeNode* l,TreeNode* r){
if(l==NULL && r==NULL) return true;
if(l==NULL || r==NULL) return false;
return (l->val == r->val) && Recur(l->left,r->right) && Recur(l->right,r->left);
}
};

也可以用迭代,可以用BFS

C++代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
queue<TreeNode*> q;
if(!root) return true;
q.push(root);
q.push(root);
while(!q.empty()){
auto t1 = q.front();
q.pop();
auto t2 = q.front();
q.pop();
if(!t1 && !t2) continue;
if(!t1 || !t2) return false;
if(t1->val != t2->val) return false;
q.push(t1->left);
q.push(t2->right);
q.push(t1->right);
q.push(t2->left);
}
return true;
}
};

(二叉树 DFS 递归) leetcode 101. Symmetric Tree的更多相关文章

  1. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  2. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  3. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

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

  4. LeetCode 101. Symmetric Tree (对称树)

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

  5. (二叉树 DFS 递归) leetcode 112. Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  6. [leetcode]101. Symmetric Tree对称树

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

  7. Java [Leetcode 101]Symmetric Tree

    题目描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  8. LeetCode 101. Symmetric Tree 判断对称树 C++

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

  9. Leetcode 101. Symmetric Tree(easy)

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

随机推荐

  1. Hadoop Yarn框架详细解析

    在说Hadoop Yarn之前,我们先来看看Yarn是怎样出现的.在古老的Hadoop1.0中,MapReduce的JobTracker负责了太多的工作,包括资源调度,管理众多的TaskTracker ...

  2. 比较器 comparable与comparator用法

    comparable 接口 Comparable<T> 类型参数:T - 可以与此对象进行比较的那些对象的类型 public interface Comparable<T> 此 ...

  3. AES 加密与解密

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Securit ...

  4. Linux内存描述之内存页面page--Linux内存管理(四)

    1 Linux如何描述物理内存 Linux把物理内存划分为三个层次来管理 层次 描述 存储节点(Node) CPU被划分为多个节点(node), 内存则被分簇, 每个CPU对应一个本地物理内存, 即一 ...

  5. #022 Python 实验课

    拍7游戏 描述 “拍7游戏”规则是:一堆人围成一圈,开始时,任意指定一人说出数字“1”后,一圈人按顺时针方向,每人按整数由小到大的顺序一人一个地报出后续数字“2”.“3”......,当遇到为“7”的 ...

  6. 初学Django项目可能会遇到的问题

    1. 出现莫名其妙的 app01 我项目中的app名字并不是app01,可是运行python manage.py makemigrations的时候总是提示app01不是已安装的app Applyin ...

  7. day10-内置模块学习(一)

    今日份目录 1.模块之间的相互调用 2.代码结构的标准化 3.os模块 4.sys模块 5.collection模块 开始今日份总结 开始今日份总结 1.模块之间的相互调用 由于一些原因,总是会调用别 ...

  8. java中的out of memory

    转:http://outofmemory.cn/c/java-outOfMemoryError java.lang.OutOfMemoryError这个错误我相信大部分开发人员都有遇到过,产生该错误的 ...

  9. nginx与fastdfs配置详解与坑

    nginx与fastdfs配置详解与坑 环境 ubantu19.04 fastdfs-5.11 fastdfs-nginx-module-1.20 libfastcommon-1.0.39 nginx ...

  10. 分布式 cephfs

    参考链接: http://docs.ceph.com/docs/mimic/cephfs/