[leetcode]_Symmetric Tree
第二道树的题目,依旧不会做,谷歌经验。
题目解释: give you a tree , judge if it is a symmetric tree.
思路:我以为要写个中序遍历(进阶学习非递归算法)什么的,Wrong Answer。
解题思路:如果 左子树的值 等于 右子树的值,并且该左子树的左子树 等于 该右子树的右子树,并且该左子树的右子树 等于 该右子树的左子树 时,该树为symmetric。(如果tree == null || tree中只有一个节点,return true)
代码:
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return isSymmetricRecursive(root.left , root.right);
}
public boolean isSymmetricRecursive(TreeNode left , TreeNode right){
if(left != null && right != null){
return left.val == right.val && isSymmetricRecursive(left.left , right.right)
&& isSymmetricRecursive(left.right , right.left);
}else if(left != null || right != null) return false;
else return true;
}
收获:关于树的求解,多考虑 递归 方法,递归 代码简单,思路直接。进阶需要学习树的非递归方法。
[leetcode]_Symmetric Tree的更多相关文章
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- leetcode第一刷_Symmetric Tree
必须承认,一開始这道题我是不会做的.由于我心目中的树遍历仅仅能用一个节点发起.多么天真而无知. 我想不通如何同一时候遍历两颗子树.由于根节点一定是一个啊.但是,作为对称轴上的它.从一開始就不应该被考虑 ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
随机推荐
- [ActionScript] AS3 涂鸦的擦除和撤销功能
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BlendMo ...
- ppm与mg/m3转换
http://wenku.baidu.com/link?url=PY0Nb09VWmBDZgLvMhaHYGJyaC6YcdYAI5XTPRkxMpDHPrs3dNxskYkKmp2F0E6Sl2H5 ...
- gomobile 真机 log 打出的日志跟踪
go mobile 开发的应用,真机调试时,我们期望看到log包打出的日志, 这时候就需要借用 Android Device Monitor 了. 我们的 go 代码中用最简单的 log.Printl ...
- OpenCV数据结构:CvMat,IplImage,CvArr的应用
最近看HS算法时,发现在新的OpenCV3.0已经移除了该算法,于是不得不看老版的.这其中涉及到了CvMat,CvArr,IplImage的使用.关于这三个结构与Mat的转换关系,总结如下: (1)派 ...
- VC让对话框显示就最大化
方法一:在OnInitDialog()函数中 ShowWindow(SW_SHOWMAXIMIZED); 初始化的时候 方法二: 当然,你可以获取屏幕大小,然后设置窗口位置/大小 //ShowWind ...
- 【OpenCV入门教程之三】 图像的载入,显示和输出 一站式完全解析(转)
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/20537737 作者:毛星云(浅墨) ...
- tomcat下部署可以访问的文件夹
项目名#文件夹名#文件夹名.xml <Context docBase="D:\文件夹名\r文件夹名"/> 例如: test#zhang#test1.xml <Co ...
- THE ONE THING PEOPLE WILL MASSIVELY OVERPAY FOR (有一个东西人们是愿意出高价购买的)
THE ONE THING PEOPLE WILL MASSIVELY OVERPAY FOR有一个东西人们是愿意出高价购买的 by GARY VAYNERCHUK 点此直达湾区日报简评 I don' ...
- EXTJS 密码确认与验证
extjs 框架是一个非常优秀的前端框架,提供了丰富的功能与炫丽的界面展示,在用 extjs 创建表单时,特别是在注册或修改密码的时候,要对密码进行确认,这里就密码确认一致性验证和大家分享自己的心得与 ...
- oracle 清除当前用户的回收站
--清除当前用户的回收站:purge recyclebin; --删除表数据truncate table --查看当前用户回收站select * from user_recyclebin t;