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

题目解释: 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. [ActionScript 3.0] AS3.0 动态加载显示内容

    可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...

  2. 【转】三种不同类型的ssh隧道

    转自:http://blog.creke.net/722.html 大家都知道SSH是一种安全的传输协议,用在连接服务器上比较多.不过其实除了这个功能,它的隧道转发功能更是吸引人.下面是个人根据自己的 ...

  3. 在RAC中,当私有网线拔了后,会怎么样?

    原文链接http://blog.mchz.com.cn/?p=4305 实际环境: OS:oel5.5_x64 ORACLE:10205 3节点rac 架设于vmware esxi虚拟机上 所需测试项 ...

  4. sql 2008 r2

    http://jingyan.baidu.com/article/6c67b1d6ca06f02787bb1ed1.html

  5. I/B/P SP/SI

    H264中I.B.P帧 I帧:只作为参考帧,采用帧内预测 B帧:以其前面的I帧或P帧和后面的P帧作为参考帧 P帧:只能以前面的I帧或P帧作为参考帧 H264中的SP SI SP和SI是H264中引入的 ...

  6. JavaScript对象的创建之工厂方法

    通过工厂的方式来创建Person对象,在createPerson中创建一个对象,然后为这个对象设置相应的属性和方法,之后返回这个对象. function createPerson(name, age) ...

  7. C# Windows Forms 事件处理顺序

    事件引发的顺序对某些Windows 窗体应用来说十分重要.当某些事件需要特别处理时(如重绘窗体的某些部分),必须知道事件在运行时的确切引发顺序.下面就应用程序和控件的生命周期中的几个重要阶段的事件顺序 ...

  8. VS2015+win10+opencv3.0整个安装过程

    LZ最近换了台新台式电脑,开始下载新VS软件,话说软件平台越新越好用,一看网上已经有VS2015版本,果断就去官网下载. 1.安装VS操作 官方网的链接如下:https://www.visualstu ...

  9. python学习笔记(递归函数)

    博主看了看递归.说的简单点就是程序里面再调用程序本身,或者是方法里面再调研方法本身.或者是函数里面再调研函数本身 用于什么场景呢,博主这里是父子节点排序,父子节点的查询 直接上代码: #!/usr/b ...

  10. SDUT 3340 数据结构实验之二叉树一:树的同构

    数据结构实验之二叉树一:树的同构 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两棵树 ...