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

题目解释: 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. DimDate

    CREATE TABLE [dbo].[DimDate]( [DateKey] int NOT NULL , [FullDate] DATE NOT NULL , [MonthNumberOfYear ...

  2. oj 中G++和C++区别(转)

    1.输出double类型时,如果采用G++提交,scanf采用%lf,prinf采用%f,否则会报错 2.使用GCC/G++的提醒: 对于64位整数, long long int 和 __int64 ...

  3. Altium designer 原理图库快速创建

    Altium designer 原理图库快速创建,原来都没发现用这个功能,最近查了一下很好用,就是通过Excel编写管脚名称再直接导入就可以了,很方便的. 1.首先在Excel创建填好对应管脚名称. ...

  4. X230上安装Yosemite/Win7-黑苹果之路

    以前曾经在X230上安装了mavericks,但因为无线网卡问题最终作罢,现在换了SSD(128G).AR9285网卡,又冲刺了一把OSX,折腾了好几天,终于成了.特做记录如下: 首先,硬盘分区问题, ...

  5. [系统] 安装Ubuntu 双系统 - 失败

    因为工作原因, 所以需要装ubuntu系统. 在网络上查了一下, 一般都是使用U盘安装. 但是由于手头上既没有U盘又没有光盘,只能用硬盘安装了. 查一下, 使用wubi安装方式从硬盘安装, 非常方便. ...

  6. 【CImg】三角形绘制算法实现

    这周的CV基础练习是简单的图形绘制:比如说矩形.三角形和圆心什么的.会发现其实矩形和圆形的实现思路都很直白,矩形只需要确认两个对角坐标就可以了,圆心只需要确认圆心和半径,接着就是简单的遍历各个像素点判 ...

  7. Unity WebSocket(仅适用于WebGL平台)

    !!!转载注明:http://www.cnblogs.com/yinlong1991/p/unity_ylwebsocket.html Unity WebSocket 使用 1.下载 YLWebSoc ...

  8. Flex 4 不同主题下容器子元素的管理方法

    Flex 下,容器主要分两类:Spark容器, Halo容器. Spark容器 Halo容器 说明 numElements numChildern 容器的子元素数量. addElement( ) ad ...

  9. EXTJS 表单提交

    EXTJS框架中,在提交表单时,可有3种方式: 方法一: 普通的 Form 提交方式, 主要使用 form.submit() 方法来将表单提交到后台,在后台是根据表单的 name 属性来获取表单中元素 ...

  10. Accessing the Deep Web: A Survey

    http://www.inf.ufsc.br/~ronaldo/deepWeb/querying/Chang-dwsurvey-cacm07.pdf