LeetCode (65):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.
首先想到的是递归法,判断过程为依次遍历每一个点如果有:
- 如果都为null或是都不为null且值相等返回true
- 一个为Null另一个不为Null返回false
- 两个都不为null但值不相等返回false
代码如下:
class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public static boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null) return true; //同时到达叶子节点
else if(p==null||q==null) return false; //不同时到达叶子则不相同
if(p.val!=q.val) return false; //节点值不同则不相同
else return isSameTree(p.left, q.left)&&isSameTree(p.right, q.right); //递归 }
//测试
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode t1=new TreeNode(1);
TreeNode nodeB=new TreeNode(2);
t1.left=nodeB; TreeNode t2=new TreeNode(1);
TreeNode nodeB1=new TreeNode(2);
t2.left=nodeB1;
System.out.println(isSameTree(t1,t2));
} }
LeetCode (65):Same tree的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- C#版 - Leetcode 65. 有效数字 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [LeetCode] 429. N-ary Tree Level Order Traversal_ Easy
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
随机推荐
- SpringMvc三大组件详解
SpringMvc框架结构图 处理器映射器:用户请求路径到Controller方法的映射 处理器适配器:根据handler(controlelr类)的开发方式(注解开发/其他开发) 方式的不同区寻找不 ...
- 在Windows上手动安装php开发环境
安装MySQL 使用官方提供提供的安装包一键安装即可. 打开 mysql,选择Windows,MSI Installer点击下载.附:最新版mysql5.7.18下载地址 点击installer安装, ...
- qt model view 编程总结
看不见的root的 QModelIndex() 是 无效的 list 和table 的index 函数中的parent参数就只要 root QModelIndex 就可以,因为没有层级概念 Model ...
- 即使关闭了nagle算法,粘包依旧存在
JAVA高级架构 https://mp.weixin.qq.com/s?src=11×tamp=1542107581&ver=1242&signature=OoktA ...
- K-means聚类算法MATLAB
以K-means算法为例,实现了如下功能 自动生成符合高斯分布的数据,函数名为gaussianSample.m 实现多次随机初始化聚类中心,以找到指定聚类数目的最优聚类.函数名myKmeans.m 自 ...
- zabbix 添加 微信、邮件 媒介详解
1:zabbix 添加 微信.邮件 媒介. 1.2:发送告警邮件 1:一次完整的监控流程可以简单描述为: Host Groups (设备组) ->Hosts ( ...
- 360UI 界面框架 即QUI框架与EXT比较
EXTJS框架是非常全面和成熟的,这是因为它发展的年头久远,并且有全世界的EXTJS爱好者为其出谋献策,它的组件库尤其是DataGrid组件无人能出其右.我在最初也考虑过使用EXTJS来做项目,学习了 ...
- Linux Tomcat部署常用命令
Linux Tomcat部署常用命令 1.连接服务器 2.进入webapps目录: cd /usr/local/tomcat8080/webapps/ 3.上传文件(war包等):rz 4.删除文件 ...
- 《FTL之垃圾回收、写放大和OP 》总结
来自 http://www.ssdfans.com/?p=1840: 写放大WA: 对空盘来说(未触发GC),写放大一般为1,即Host写入多少数据,SSD写入闪存也是多少数据量(这里忽略SSD内部数 ...
- PAT 1122 Hamiltonian Cycle[比较一般]
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...