leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
给出两个二叉树,写一个方法判断两个二叉树是否相等,
如果两个二叉树相等,说明结构相同并且每个节点的值相同。
如果两个节点都为空,则说明相同,返回true,
判断两个根节点的值不同,或者一个为空一个不为空,说明两个树不相同,返回false。
然后再递归左右节点,如果有一个为false或者两个都为false,返回false。
时间O(N), 空间O(h)。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
leetcode 100. Same Tree的更多相关文章
- LeetCode 100. Same Tree (判断树是否完全相同)
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- LeetCode 100. Same Tree (相同的树)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [LeetCode] 100. Same Tree 相同树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- leetcode 100 Same Tree ----- java
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- Java [Leetcode 100]Same Tree
题目描述: Given two binary trees, write a function to check if they are equal or not. Two binary trees a ...
- [Leetcode]100. Same Tree -David_Lin
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- (二叉树 递归 DFS) leetcode 100. Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)
描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...
随机推荐
- Maven中的dependencyManagement 意义
1.在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器. 2.pom.xml文件中,jar的版本判断的两种途径 1:如果dependenci ...
- angularjs中$watch监听model(对象属性、对象)变化
昨天看了一下教学视频,学到了有关$watch的用法,想到最近做的一个页面中有个select下拉选项(select中的值变化了,则后面input中的值也跟着相应的变化),不知是否可以使用$watch来代 ...
- 兼容ie6及一下版本的自适应
<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" /> <meta ...
- jquery控制滚动条改变上面固定(fixed)导航条或者搜索框的属性
<script type="text/javascript"> $(document).ready(function(){ $(window).scroll( func ...
- 史上最全的Linux常用命令
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- OC面向对象特性: 继承
基础知识 1.标识符是有字母,数字,下划线组成的. 2.首字母只能是字母,下划线,不能为数字. 3.标识符要做到见名之意. 4.标识符不能使用已定义的关键字和预定义标识符. 继承 继承:子类可以直接访 ...
- 9月23日JavaScript作业----日期时间选择
作业二:日期时间选择 <div style="width:600px; height:100px;"> <select id="year"&g ...
- Java——文件选择框:JFileChooser
import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import j ...
- OpenGL Code Resources
https://www.opengl.org/wiki/Code_Resources http://ogldev.atspace.co.uk/
- ConterReplaceBehavior.class.php模板内容替换,如__PUBLIC__
ConterReplaceBehavior.class.php查找 __PUBLIC__ protected function templateContentReplace($content) { / ...