LeetCode——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.
考虑使用深度优先遍历的方法,同时遍历两棵树,遇到不等的就返回。
代码如下:
/**
* Definition for binary tree
* 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 p==q;
}
if(p.val != q.val){
return false;
}
return isSameTree(p.left,q.left)&& isSameTree(p.right,q.right);
}
}
LeetCode——Same Tree(判断两棵树是否相同)的更多相关文章
- [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树
将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...
- LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- 判断两棵树是否相等 leecode
很简单 提交代码 https://oj.leetcode.com/problems/same-tree/ iven two binary trees, write a function to chec ...
- [LeetCode] Same Tree 判断相同树
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 笔试算法题(27):判断单向链表是否有环并找出环入口节点 & 判断两棵二元树是否相等
出题:判断一个单向链表是否有环,如果有环则找到环入口节点: 分析: 第一个问题:使用快慢指针(fast指针一次走两步,slow指针一次走一步,并判断是否到达NULL,如果fast==slow成立,则说 ...
- [LeetCode] Equal Tree Partition 划分等价树
Given a binary tree with n nodes, your task is to check if it's possible to partition the tree to tw ...
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- element ui改写实现两棵树
使用element ui组件库实现一个table的两棵树的效果 效果如下,左边树自动展开一级,右边树默认显示楼层,然后可以一个个展开 代码如下 <el-table :data="rel ...
- [剑指Offer]判断一棵树为平衡二叉树(递归)
题目链接 https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=0&tqId=0&rp=2&a ...
随机推荐
- Metronic 与 VS2013/2015 合作开发
Metronic 与 VS2013/2015 合作开发 去年购买了一个:METRONIC (http://www.keenthemes.com/) ,最近下了最新的版本:V3.7 ,解压缩后,目录 ...
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- POJ 2570(floyd)
http://poj.org/problem?id=2570 题意:在海底有一些网络节点.每个节点之间都是通过光缆相连接的.不过这些光缆可能是不同公司的. 现在某个公司想从a点发送消息到b点,问哪个公 ...
- NGUI 图片变灰
效果图 1.先准备好一个变灰shader.代码如下 Shader "Custom/Gray" { Properties { _MainTex ("Base (RGB), ...
- Selenium FF WebDriver运行时开启firebug的2种方式
上一次我实测FF webdriver 加载firefoxhttp://www.cnblogs.com/tobecrazy/p/3997375.html 那么问题就来了,既然能加载firebug能否在运 ...
- json格式
$.post('text.action',{....},function(datas){ var name=datas.data[0].name; }); 如果是多个还可以用循环获取.$.post(' ...
- EMIS系统运行时提示【无法验证发行者,您确实要运行此软件吗? 】
无法验证发行者,您确实要运行此软件吗? 遇到这个提示你怎么办? 运行 gpedit.msc 进入组策略用户配置 ==>管理模板==> winows组件 ==> 附件管理器在 &quo ...
- [Android Studio导入第三方类库方法] Error:(19, 23) 错误: 程序包org.apache.http不存在
本文主要参考以下链接: http://m.blog.csdn.net/blog/BoxRice/48575027 https://drakeet.me/android-studio http://ww ...
- nyoj116_士兵杀敌(二)_树状数组
士兵杀敌(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常 ...
- manage account
#!/bin/bash # #Delete_user - Automates the steps to remove an account # ############################ ...