第二道树的题目,依旧不会做,谷歌经验。

题目解释: 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的更多相关文章

  1. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  2. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  3. leetcode第一刷_Symmetric Tree

    必须承认,一開始这道题我是不会做的.由于我心目中的树遍历仅仅能用一个节点发起.多么天真而无知. 我想不通如何同一时候遍历两颗子树.由于根节点一定是一个啊.但是,作为对称轴上的它.从一開始就不应该被考虑 ...

  4. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  5. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  6. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

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

  8. [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 ...

  9. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

随机推荐

  1. wait(0)

    public final synchronized void join(long millis) throws InterruptedException { long base = System.cu ...

  2. CRM报表打印

    删除路径下的文件 C:\Windows\Downloaded Program Files\rsclientprint.dll路径下的这个dll文件,重新登录crm选择一个面单点击打印按钮重新安装插件

  3. rhel5 新建用户提示:the home directory already exists.

    rhel5 新建用户提示:the home directory already exists.(as4不存在这个问题) 环境如下: [oracle@rhel5 ~]$ df -hFilesystem  ...

  4. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  5. ubuntu14.04配置Hive1.2.1

    1.添加环境变量:vi ~/.bashrc #HIVE VARIABLES START export HIVE_HOME=/usr/local/hive-1.2.1 export PATH=$PATH ...

  6. SparkSQL使用之Spark SQL CLI

    Spark SQL CLI描述 Spark SQL CLI的引入使得在SparkSQL中通过hive metastore就可以直接对hive进行查询更加方便:当前版本中还不能使用Spark SQL C ...

  7. struts2下实现的json传递list,object。

    必须的jar: java bean: package upload.progress.action; public class music { private String name; private ...

  8. 哪项技术可以用在WEB开发中实现会话跟踪实现?

    HTTP是“无状态”协议:客户程序每次读取 Web 页面,都打开到 Web 服务器的单独的连接,并且,服务器也不自动维护客户的上下文信息.即使那些支持持续性 HTTP 连接的服务器,尽管多个客户请求连 ...

  9. Linux-wget/tar/ln 函数

    1. 获取软件包,可以使用wget的方式, ubuntu可以使用apt-get source来获取源代码 wget 是一个在网络上进行下载的简单而强大的自由软件,支持HTTP,HTTPS,FTP协议, ...

  10. 【LeetCode】11. Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...