[leetcode]_Same Tree
第一次遇见Tree的题,拿到心慌,网上查了解题思路。写完就三行。。
最近努力学习一句话,学会喜欢自己。
题目:give two tree , you must judge if they are the same tree. ensure they are the same tree structure and node value.
开始思路:我想保存下tree的中序遍历,来判断tree是否相等,但是这块代码不会写。
解题思路:递归遍历两颗树,比较节点值是否相等。如果相等,则递归比较两颗树的leftSubTree 和 rightSubTree。注意:当出现任意一棵为Null 而另一棵不为Null 时,无条件返回false.
code:(Java)
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null)
return true; // both trees are null
if(p == null && q != null || p != null && q == null || p.val != q.val)
return false;
// Any tree is null or their node value is different
return isSameTree(p.left , q.left) && isSameTree(p.right , q.right);
// recursive compare their leftSubTree and rightSubTree
}
[leetcode]_Same 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第一刷_Same Tree
回来更博客的时候才发现.这道题不是跟推断树是不是对称的很相像吗.这个也是用了两个指针同一时候递归啊,有时候思维的局限真可笑. class Solution { public: bool isSameT ...
- [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 ...
随机推荐
- android Studio 快捷键(转载)
http://www.cnblogs.com/ut2016-progam/p/5627142.html http://blog.csdn.net/shulianghan/article/details ...
- 把Nginx加为系统服务(service nginx start/stop/restart)
1.编写脚本,名为nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - ...
- CodeForces 604D 【离散数学 置换群】
题意: 给你一个方程,方程太变态不打,给你一个p一个k,p保证是大于等于3的质数,k保证在0~p-1之间的整数.要求对应函数的定义域在0~p-1值域为0~p-1的子集,求这样的函数有多少个... 分析 ...
- Sqoop导入mysql数据到Hbase
sqoop import --driver com.mysql.jdbc.Driver --connect "jdbc:mysql://11.143.18.29:3306/db_1" ...
- T4 模板的调试方法,方便大家遇到问题自己快速定位和优化
T4 模板的调试方法,方便大家遇到问题自己快速定位和优化 :1. .ttinclude文件的第一行修改为 <#@ template language="C#" debug=& ...
- 《点石成金-访客至上的web和移动可用性设计秘籍》读书笔记
简介 作者Steve Krug,惯例先去了解一下本书的作者,发现书中介绍的并不多,百度一下后发现这本书比作者出名.好吧,百度就是这样子,作者自称web可用性咨询师,手上这本书是第三版再版,第一版2 ...
- 浅谈如何使用代码为MP3文件写入ID3Tags
作者:郑童宇 GitHub:https://github.com/CrazyZty 1.前言 做了三年左右的Android开发,一直没写过博客,最近正好打算换工作,算是闲一些,就将以前开发所遇到的一些 ...
- 慕课网-安卓工程师初养成-2-12 如何在Java中使用注释
来源:http://www.imooc.com/code/1274 在编写程序时,经常需要添加一些注释,用以描述某段代码的作用. 一般来说,对于一份规范的程序源代码而言,注释应该占到源代码的 1/3 ...
- PMP考试--成本管理中常用的概念
如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 净现值(NPV) Net Present Value 在项目计算期内,按行业基准 ...
- 缓存之Memcached
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...