[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 ...
随机推荐
- 防止ARP欺骗的方法!!!
防止ARP欺骗的方法!!! 根据鄙人上网经常掉线,怀疑可能是某人使用网络剪刀手,网络执行官,局域网终结者等工具.经过搜索有关资料,有了一点点防范经验,借以参考~! 一 首先我们来了解下这类攻击工具的原 ...
- WCF和WebService中获取当前请求报文的方法
WCF中: 1. 在hosting WCF的web.config中加入: <system.serviceModel> <serviceHostingEnvironment aspNe ...
- BestCoder Round #79 (div.2)
1001.没推到题解那么细,枚举一下也可以.用通分可以避免小数精度问题. #include<iostream> #include<stdio.h> using namespac ...
- 自动生成Makefile时,关于Makefile.am编写
最近编译一个项目的程序时,二十几个源代码文件放在六个文件夹中,而且各个文件中头文件互相包含.以前写过编译这样组织的源码的makefile,所以这次也就直接写了. 确实因为各个文件间的头文件互相包含,造 ...
- QWizard中运行时默认按钮显示英文问题
QWizard中运行时默认按钮在编译前设计界面的时候是显示中文的,运行的时候就变成英文了.. 后来是发现国际化的时候有问题,解决办法如下: 在main.cpp里加: QTranslator* tran ...
- gulp - connect
Gulp plugin to run a webserver (with LiveReload) Install npm can help us to install the plugin. PS C ...
- git 如何恢复只是提交到本地的文件(或者commit)
今天早上傻逼了,把四天的代码commit到了本地,然后fetch一下,然后就全没了,不过git还是挺强大的 参考:http://blog.163.com/jiams_wang/blog/static/ ...
- 【LeetCode】18. 4Sum
题目: 思路:这题和15题很像,外层再加一个循环稍作修改即可 public class Solution { public List<List<Integer>> fourSu ...
- 【LeetCode】6. ZigZag Conversion 锯齿形转换
题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...
- 慕课网-安卓工程师初养成-1-6 MyEclipse的使用简介
来源 http://www.imooc.com/video/1414 http://www.my-eclipse.cn/ MyEclipse 2014 官方版下载地址 声明:MyEclipse 20 ...