Symmetric Tree 深度优先搜索
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool dfscheck(TreeNode *leftNode,TreeNode *rightNode){
if(leftNode==NULL &&rightNode==NULL)
return true;
if(leftNode==NULL || rightNode==NULL)
return false;
return leftNode->val==rightNode->val && dfscheck(leftNode->left,rightNode->right) && dfscheck(leftNode->right,rightNode->left);
}
bool isSymmetric(TreeNode *root) {
if(root==NULL)
return true;
return dfscheck(root->left,root->right);
}
};
Symmetric Tree 深度优先搜索的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)
Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...
- Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)
Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
- 初涉深度优先搜索--Java学习笔记(二)
版权声明: 本文由Faye_Zuo发布于http://www.cnblogs.com/zuofeiyi/, 本文可以被全部的转载或者部分使用,但请注明出处. 上周学习了数组和链表,有点基础了解以后,这 ...
- 对无向图的深度优先搜索(DFS)
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 如何对无向图进行深度优先搜索 的idea 并用源代码加以实现: 0.2) 本文还引入了 背向边(定义见下文 ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)
Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...
随机推荐
- [转载] OpenCV2.4.3 CheatSheet学习(四)
五.数据的输入和输出 1. 将数据写入YAML(或XML) 注意,在OpenCV中,无论读写,文件的格式均由指定的后缀名确定.示例: FileStorage fs("test.yml&quo ...
- springmvc:配置解决中文乱码的过滤器
在web.xml中配置以下内容: <!--配置解决中文乱码过滤器--> <filter> <filter-name>characterEncodingFilter& ...
- JAVA面试常见问题之Redis篇
Redis为单线程 1.Redis 有哪些数据类型 String 哈希 list set 有序set 2.Redis 内部结构 参考:https://www.cnblogs.com/chenpingz ...
- Django项目:CRM(客户关系管理系统)--45--37PerfectCRM实现King_admin添加用户时密码加密
#views # ————————02PerfectCRM创建ADMIN页面———————— from django.shortcuts import render # ————————04Perfe ...
- 用javascript的classList代替jquery的class操作
javascript的className操作方法比较难受,他获取的是一个连续的字符串 必须要用split拆分开,好多人因此想去用jquery的addClass,removeClass,hasClass ...
- 关于网上大量对Vue双向绑定的错误理解
对于Vue的双向绑定,现在基本是个前端都听说过,面试官也喜欢问这个问题.但对于Vue双向绑定的原理,掘金.博客园和segmentfault等技术社区充斥着大量的错误文章.这些文章的题目基本样子如下 “ ...
- java swing同时向jlabel添加图片和文字,并且设置文字的位置
jLabColor.setVerticalTextPosition(JLabel.TOP);//靠上 jLabColor.setHorizontalTextPosition(JLabel.CENTER ...
- jnhs中国的省市县区邮编坐标mysql数据表
https://blog.csdn.net/sln2432713617/article/details/79412896 -- 1.之前项目中需要全国的省市区数据,在网上找了很多,发现数据要么不全,要 ...
- font-family:黑体;导致css定义全部不起作用
css文件里font-family: "黑体";这句会导致后面的css定义全部不起作用了. 只要把font-family: "黑体"; 改成 font-fami ...
- git update-index --assume-unchanged忽略跟踪
Git 忽略已跟踪文件的改动 git update-index --assume-unchanged Git之本地忽略 这个分两种情况: 本地永久忽略,效果的gitignore一样,只不过不适于写到g ...