(二叉树 递归 DFS) leetcode 100. Same Tree
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Example 1:
Input: 1 1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true
Example 2:
Input: 1 1
/ \
2 2 [1,2], [1,null,2] Output: false
Example 3:
Input: 1 1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false
---------------------------------------------------------------------------------------------
这个题就是判断两个二叉树是否相等。可以用DFS,也可以用BFS。
emmmmm,按理来说用DFS应该是简单的,至少代码好写,可是,我怎么感觉DFS比BFS还难???? C++代码:
/**
* Definition for a binary tree node.
* 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;
if(p == NULL || q == NULL) return false;
if(p->val != q->val) return false;
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
};
(二叉树 递归 DFS) leetcode 100. Same Tree的更多相关文章
- LeetCode 100. Same Tree (判断树是否完全相同)
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)
描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...
- 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 ...
- Leetcode 100 Same Tree 二叉树
就是判断两棵树的值和结构是否相同 注意:要判断是否所有的树节点是否为NULL /** * Definition for a binary tree node. * struct TreeNode { ...
- [LeetCode] 100. Same Tree 相同树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
随机推荐
- 智能POS如何获取日志&数据库文件
使用Teamviewer连接安卓机器,文件传输 ================================================================== 智能POS查看数据 ...
- UltraEdit 注册机
IDM-All-Products-KeyGen-v3.6UltraEdit 注册机 1.断开网络连接,运行UltraEdit软件后,点击“帮助”—“注册/激活”; 2.许可证ID填写“zd423”.密 ...
- linux内核IDR机制详解【转】
这几天在看Linux内核的IPC命名空间时候看到关于IDR的一些管理性质的东西,刚开始看有些迷茫,深入看下去豁然开朗的感觉,把一些心得输出共勉. 我们来看一下什么是IDR?IDR的作用是什么呢? 先来 ...
- Linux学习历程——Centos 7 grep命令
一.命令简介 grep 命令用于在文本中执行关键词搜索,并显示匹配的结果. 由于grep命令参数很多,这里只列出一些常用的参数. 参数 作用 -b 将可执行文件当作文本文件来搜索 -c 仅显示找到的行 ...
- vmware station-ubuntu18.04 共享剪贴板
辞职在家休息,买了台新电脑,装个虚拟机,安装visual studio, android studio, qt, everything, noptepad++,hbuilder,ditto,xx-ne ...
- 浏览器仿EXCEL表格插件 版本更新 - 智表ZCELL产品V1.3发布
智表(zcell)是一款浏览器仿excel表格jquery插件.智表可以为你提供excel般的智能体验,支持双击编辑.设置公式.设置显示小数精度.下拉框.自定义单元格.复制粘贴.不连续选定.合并单元格 ...
- Spring注解定时器使用
一.首先要配置我们的spring-service.xml 1.xmlns 多加下面的内容 xmlns:task="http://www.springframework.org/schema/ ...
- Python开发【socket篇】解决粘包
客户端 import os import json import struct import socket sk = socket.socket() sk.connect(('127.0.0.1',8 ...
- java 下载word freemaker
网上有很多优质的博文了,这里这篇博客就是记录一下字自己,写demo的历程,坑和收获 在java程序中下载word 有6中方式,此处省略(嘻嘻),不过大家公认的是 freemaker 和 PageOff ...
- shell杀死指定端口的进程
杀死端口代码如下: lsof -i: kill - PID 上面的与下面的代码作用相同. 命令如下所示(这种方式更自动化): kill - $(netstat -nlp | grep : | awk ...