[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.
二叉树相同:一、结构相同;二、对应节点上的值相同。
思路:层次遍历,维护两个队列,判断对应节点的值是否相等。值得注意的有:一、节点均为空时,再次循环取队首元素,有两种情况,i)q、p就为空,则不满足再次循环条件时,此时q==p为空,两树相同;ii) 到叶节点时,将其左右孩子压入队列时,对应的均为空,则可以再次循环,取队首元素,重新对比;二、有一个节点为空时,因为第一种情况,排除了两者都为NULL,所以这里只是其中有一个为NULL,则返回false;三、对应节点值不等,则返回false;然后依次将两树的左右孩子压入队列中继续循环。
值得注意:若是循环体中,将两树的左右孩子压入队列中,加判断左右孩子是否存在,存在则压,否则不压,则,while条件中既不能用||也不能用&&,因为,||时,会对空队列取首元素,&&时,会造成不能而等,如{1,1}和{1,1,1},所以不能压入时加if判断。
当然可以改变代码的写法,这样可以加if判断,见Grandyang的博客。别的遍历方式应该也能达到同样的实现判断两树是否相同。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q)
{
queue<TreeNode *> pQ;
queue<TreeNode *> qQ; pQ.push(p);
qQ.push(q);
while( !pQ.empty()&& !qQ.empty()) //这里||和&&都可以AC,
{
TreeNode *pNode=pQ.front();
TreeNode *qNode=qQ.front();
pQ.pop();
qQ.pop(); //同时不存在
if(pNode==NULL&&qNode==NULL)
continue;
//有一个不存在
if(pNode==NULL||qNode==NULL)
return false; if(pNode->val !=qNode->val)
return false; pQ.push(pNode->left);
pQ.push(pNode->right);
qQ.push(qNode->left);
qQ.push(qNode->right);
}
return true;
}
};
方法二:
递归大法。终止条件上见;递归式,要注意的是,左右子树必须同时一样才行,所以要用&&
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q)
{
if(p==NULL&&q==NULL) return true;
else if(p==NULL||q==NULL) return false;
else if(p->val !=q->val) return false; return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}
};
[Leetcode] Same tree判断是否为相同树的更多相关文章
- [LeetCode] Same Tree 判断相同树
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- hdu 1325 判断有向图是否为树
题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...
- 【js】Leetcode每日一题-叶子相似的树
[js]Leetcode每日一题-叶子相似的树 [题目描述] 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7 ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
随机推荐
- Spring常见面试题
本文是通过收集网上各种面试指南题目及答案然后经过整理归纳而来,仅仅是为了方便以后回顾,无意冒犯各位原创作者. Spring框架 1. 什么是Spring? Spring 是个java企业级应用的开源开 ...
- 46.VUE学习之--组件之使用动态组件灵活设置页面布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Javaweb——四则运算---18.11.01
---恢复内容开始--- test.jsp <%@ page language="java" contentType="text/html; charset=utf ...
- python2.X与python3.X爬虫常用的模块变化对应
python2 python3 import urllib2 import urllib.request,urllib.error import urllib.request,urllib.error ...
- linux c fprintf()
#include<stdio.h> #include<unistd.h> #include<time.h> int main(int argc,char *argv ...
- WPF 构建无外观(Lookless)控件
原文:WPF 构建无外观(Lookless)控件 构建一个用户可以使用Template属性设置外观的WPF控件需要以下几步 1.继承自System.Windows.Controls.Control 2 ...
- 使用maven插件生成grpc所需要的Java代码
1.首先需要编写自己需要的.proto文件,本文重点不在这里,.proto可以参考grpc官方例子 https://grpc.io/docs/quickstart/java.html 2.创建自己的J ...
- vs调试代码的时候断点无法命中
https://blog.csdn.net/xxdddail/article/details/18696399 该链接提供的解决方案主要是如下图片:禁用 图片标记的这个选项即可:
- Git 上传本地仓库到码云
一.将本地的项目上传到码云 1.码云上创建一个项目 testgit (名字随你) 2.本地创建一个文件夹D:/testgit,然后使用git bash 3.cd 到本地文件夹中D:/testgit 4 ...
- Page Object 设计模式介绍
Page Object 是 Selenium 自动化测试项目开发实践的最佳设计模式之一,Page Object 的主要体现于对界面交互细节的封装,这样可以使测试案例更关注与业务而非界面细节,提高测试案 ...