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] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
随机推荐
- LeetCode 笔记系列五 Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- Freebsd下用pureftpd配置FTP服务器
pureftpd安装配置简明说明 1.下载 http://sourceforge.net/projects/pureftpd/ 最新版本是pure-ftpd-1.0.16a.tar.bz2 BSD自 ...
- SpringBoot 之数据访问
1. Spring Boot 与 JDBC 默认使用 org.apache.tomcat.jdbc.pool.DataSource 数据源; // application.yml spring: da ...
- Django logging模块
一.Django logging配置 1.在setting.py中配置 # 日志文件存放路径 BASE_LOG_DIR = os.path.join(BASE_DIR, "log" ...
- 【opencv入门篇】 10个程序快速上手opencv【下】
导言:本系列博客目的在于能够在vs快速上手opencv,理论知识涉及较少,大家有兴趣可以查阅其他博客深入了解相关的理论知识,本博客后续也会对图像方向的理论进一步分析,敬请期待:) 上篇传送:http: ...
- HDU1115&&POJ1385Lifting the Stone(求多边形的重心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1115# 大意:给你个n,有n个点,然后给你n个点的坐标,求这n个点形成的多边形的重心的坐标. 直接套模 ...
- Linux系统——文件系统与LVM 逻辑
格式化命令 mkfs. mkswap mkfs格式化数据磁盘 # mkfs -t ext4 /dev/sdb1 # mkfs.ext4 /dev/sdb1 -t 指定格式化文件类型 -b 指定bloc ...
- discuz debug下载地址
Discuz!X会自带Debug模式,主要是用于排查性能问题.垃圾站建议当如果您的站点调试完之后,一定要把debug模式关闭,以免其他用户会获取到一些敏感信息.在调试过程中,config文件配置的 ...
- PKU 3020 Antenna Placement(拆点+最小边覆盖)(最大匹配)
题目大意:原题链接 一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使得所有的城市都覆盖无线? 提示:看清楚题目,' ...
- IOS开发如何入门
说到 iOS 开发,自己学得也很浅.不过至少独立一人完成了一个应用的开发到项目上线整个过程.分享一下自己的建议和想法. 首先建议阅读 Start Developing iOS Apps Today,你 ...