[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 ...
随机推荐
- [SQL]SQL语言入门级教材_跟我学SQL(六)
跟我学SQL:(一)数据查询 且不说你是否正在从事编程方面的工作或者不打算学习SQL,可事实上几乎每一位开发者最终都会遭遇它.你多半还用不着负责创建和维持某个,但你怎么着也该知道以下的一些有关的SQL ...
- (转)Java DES 与Base64
原文地址http://blog.csdn.net/tomatozq/article/details/20773559 1,DES /** * 解密 * @param message * @param ...
- Oracle corrupt block(坏块) 详解
转自:http://blog.csdn.net/tianlesoftware/article/details/5024966 一. 坏块说明 1.1 相关链接 在看坏块之前,先看几个相关的链接,在后面 ...
- Goldengate trial队列维护
查看进程信息: Info replicat_name $Info replicat_name showch 注: 可以查看到详细的关于checkpoint的信息,用于查看GoldenGate进程处 ...
- JSON.stringify
$(document).ready(function (){var things =[{ id:1, color:'yellow'},{ id:2, color:'blue'},{ id:3, col ...
- 一个构建XML对象的js库
初学javascript,学习中用到在IE中建立XML对象,于是写了一个简单的“库”.因为水平所限,肯定会有不恰当的地方,欢迎指正. 如果大家有知道现存的更好的东西,非常希望大家能将它推荐给我. 代码 ...
- 一个简单的AJAX实例
创建一个简单的XMLHttpRequest,从一个TXT文件中返回数据. 来源于菜鸟教程 <!DOCTYPE html><html><head><meta c ...
- 剑指Offer:面试题7——用两个栈实现队列(java实现)
题目描述:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 首先定义两个栈 Stack<Integer> stack1 = new Stack<I ...
- js实现简单的滑动门和tab选项卡
思想:首先定义三个选项卡,可以用任何标签,只要如下图, 一共有三个ul,第一个ul给一个class,因为默认第一个选项卡的内容显示出来, 其他两个ul display:none: 当我鼠标移到第二个 ...
- 关于javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
今天遇到这样一个异常: 严重: Servlet.service() for servlet jsp threw exceptionjavax.servlet.jsp.JspTagException: ...