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) 深度优先搜索的解题详细介绍,点击 ...
随机推荐
- python简单爬豆瓣电影排名
爬豆瓣电影 网站分析: 1 打开https://movie.douban.com,选择 [排行榜],然后随便选择一类型,我这里选择科幻 2 一直浏览网页,发现没有下一的标签,是下滑再加载的,可 ...
- web api中允许跨域访问
①添加owin的引用 ②添加owin.Cors的引用 ③在WebApiConfig中添加 config.EnableCors(new EnableCorsAttribute("*" ...
- Matlab---length函数
1.length函数:计算向量或矩阵的长度 2.用法说明 y = length(x) 函数计算指定向量或矩阵的长度y.如果参数变量x是向量,则返回其长度:如果参数变量是非空矩阵,则length(x)与 ...
- nodejs+express 初学(二)
开发首先要先选一个适合的IDE,经过上网查找最后还是决定用Webstorm,因为比较好用 1.我下载的版本是 2.然后就是新创建一个nodejs项目了 3.确定后就会在项目中生成新的项目,结构如下 目 ...
- css 一行或多行文字溢出以...的形式隐藏
一行文字溢出以...形式隐藏 我需要隐藏... css代码如下: white-space:nowrap; text-overflow:ellipsis; overflow:hidden; 多行文字溢出 ...
- struts2-OGNL表达式-OGNL与Struts2的结合
1 OGNL表达式 OGNL:对象视图导航语言. ${user.addr.name} 这种写法就叫对象视图导航.OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能. 1.1 使用OGNL 导包 ...
- Leetcode461Hamming Distance汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = 1, y ...
- windows console 控制台自启动
var fileName = Assembly.GetExecutingAssembly().Location; System.Diagnostics.Process.Start(fileName);
- 洛谷P3306 随机数生成器
题意:给你一个数列,a1 = x,ai = (A * ai-1 + B) % P,求第一个是t的是哪一项,或者永远不会有t. 解:循环节不会超过P.我们使用BSGS的思想,预处理从t开始跳√P步的,插 ...
- TZ_16_Vue的v-for、v-if、v-show、v-bind、watch
1.v-for 遍历数据渲染页面是非常常用的需求,Vue中通过v-for指令来实现. 1>遍历一个users数组 <!-- ve-for --> <ul> <li ...