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] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
随机推荐
- the age of the TCP connection TCP Slow Start
w防止网络过载和拥塞 HTTP The Definitive Guide The performance of TCP data transfer also depends on the age of ...
- navicat导入数据出现中文乱码
1.问题描述: excel转为csv后,导入英文时没错.但中文就是显示的乱码. 2.解决方法: 将csv用记事本打开,调整编码格式为UTF-8, 点击保存覆盖掉原文件,然后再 导入,乱码问题就解决
- Zipline Risk and Performance Metrics
Risk and Performance Metrics 风险和性能指标 The risk and performance metrics are summarizing values calcula ...
- 隐藏Apache、nginx和PHP的版本号的配置方法
最近提示说有漏洞,暴露apache.nginx和php的版本号.网上搜了下,整理的方法如下: 首先说apache 在http.conf文件里添加下面两行,默认是没有的 ServerSignature ...
- 6.2 - BBS + BLOG系统
一.简介 博客系统开发: 1.注册,登录,首页 2.个人站点,分组:(分类,标签,归档)3.文章详细页4.点赞,踩灭5.评论楼,评论树6.后台管理,发布文章,文件上传7.BeautifulSoup8. ...
- 软中断和tasklet介绍
今天看了下tasklet,重点分析了其和软中断的关系,特此记录 关于软中断,在之前的中断文章中已经有所介绍,这里就不多说了,只是说明下,系统中默认支持32种软中断,而实际上系统定义的软中断仅有以下几种 ...
- python插入记录后获取最后一条数据的id
python插入记录后取得主键id的方法(cursor.lastrowid和conn.insert_id()) 参考:https://blog.csdn.net/qq_37788558/article ...
- MySQL5.7密码安全策略(转)
环境介绍:CentOS 6.7 MySQL版本:5.7.11 1.查看现有的密码策略 mysql> SHOW VARIABLES LIKE 'validate_password%';参数解释:1 ...
- POJ3903Stock Exchange&&POJ1631Bridging signals最长上升子序列 &&POJ1887Testing the CATCHER(最长下降子序列)(LIS模版题)
题目链接:http://poj.org/problem?id=3903 题目链接:http://poj.org/problem?id=1631 题目链接:http://poj.org/problem? ...
- SDUT3146:Integer division 2(整数划分区间dp)
题目:传送门 题目描述 This is a very simple problem, just like previous one. You are given a postive integer n ...