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

/**
* 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 *lhs, TreeNode *rhs)
{
if (NULL == lhs&&NULL == rhs)return true;
if (NULL!=lhs&&NULL!=rhs&&lhs->val == rhs->val)
{
return isSymmetric(lhs->right, rhs->left) && isSymmetric(lhs->left, rhs->right);
}
return false;
}
bool isSymmetric(TreeNode* root) {
if (!root)return true;
return isSymmetric(root->left, root->right);
}
};

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 e ...

  4. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

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

  5. leetcode 101 Symmetric Tree ----- java

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

  6. Java [Leetcode 101]Symmetric Tree

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

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

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

  8. Leetcode 101. Symmetric Tree(easy)

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

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

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

随机推荐

  1. Linux下升级python

    本文的Linux系统为CentOS 7 64 在Linux系统的下载文件夹中邮件打开终端,输入命令: wget http://www.python.org/ftp/python/3.4.4/Pytho ...

  2. java 实现mysql数据库导出

    package com.zbb.util; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStre ...

  3. Angular学习(4)- 数组双向梆定

    示例: <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 4< ...

  4. 程序员书单_java学习基础编程篇

    Java程序设计语言.(美国)阿诺德.清晰版 http://download.csdn.net/detail/shenzhq1980/9076093 JAVA2核心技术第1卷.基础知识7th.part ...

  5. 【python】filter,map,reduce和lambda函数介绍

    filter(function, iterable)map(function, iterable)reduce(function, sequence) filter将 function依次作用于ite ...

  6. ios8 ios7 tableview cell 分割线左对齐

    ios8中左对齐代码 //加入如下代码 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cel ...

  7. Android五:Activity

    生命周期: onCreate onStart onResume onPause:在该状态如果有优先级更高的程序,那此进程可能被kill;如果是被重新执行,则回到onResume状态. onStop : ...

  8. 嵌入式jetty

    一.maven依赖 pom配置 <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId&g ...

  9. 理解Socket编程【转载】

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  10. (C/C++) Interview in English - Threading

    Q. What's the process and threads and what's the difference between them? A.  A process is an execut ...