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 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
分析:
给定两个二叉树,判断他们是否相同,相同的二叉树拥有相同的结构,且节点的值也要相等。
递归求解。对于两个节点是否相同,有如下情况,当两个节点的左右节点均为空时返回true,如果两个节点,有一非空时,返回false,如果两个节点值不同时,返回false,之后递归求解节点的左右节点。
程序:
/**
* 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 == nullptr && q == nullptr) return true;
if(p == nullptr || q == nullptr) return false;
if(p->val != q->val) return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
LeetCode 100. Same Tree相同的树 (C++)的更多相关文章
- 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相同的树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- [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 ...
- [LeetCode]100. Same Tree判断树相同
dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...
- 【LeetCode】Same Tree(相同的树)
这道题是LeetCode里的第100道题. 这是题目: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- C语言前置知识汇编基础--总线【地址总线,控制总线,数据总线】练习题
寻址就是用一个数字表示一个字节存储单元的地址,而不是表示这个存储单元中的内容,所以不用乘以8. 1. 1个CPU的寻址能力为8KB,那么它的地址总线宽度是多少? 8*1024=8192 共有8 ...
- 重构与模式 (Joshua Kerievsky 著)
第1章 本书的写作缘由 第2章 重构 第3章 模式 第4章 代码坏味 第5章 模式导向的重构目录 第6章 创建 第7章 简化 第8章 泛化 第9章 保护 第10章 聚集操作 第11章 实用重构 参考文 ...
- antdmobile组件思维导图
- Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(五)
直接贴代码了: NewsInfo 实体类: public class NewsInfo { public int NewsInfoId { get; set; } public string News ...
- kubernetes 之一些报错
1.kubelet与docker的Cgroup Driver不一致导致的报错 7月 :: kubeadm-master kubelet[]: W0701 :: watcher.go:] Error w ...
- Spring Cloud 新一代Web框架微服务翘楚(一)
序言 springcloud是微服务架构的集大成者,将一系列优秀的组件进行了整合.基于springboot构建,对我们熟悉spring的程序员来说,上手比较容易. 通过一些简单的注解,我们就可以快速的 ...
- WPF树形菜单--递归与非递归遍历生成树结构的集合
一.新建了WPF项目作为测试,使用TreeView控件进行界面展示. 第一步创建实体类TreeEntity: public class TreeEntity { private int _mid; p ...
- 4、Ext.NET 1.7 官方示例笔记 - 树
<%@ Page Language="C#" %> <%@ Import Namespace="System.Collections.Generic&q ...
- git stash与git commit的区别
问题的出现 写这篇文章的缘由是在工作中初次使用Git的时候遇到了一个奇怪的现象,即每次提交代码的时候,如果没有及时拉取代码就会导致本地库的代码不是最新的,这样自己修改代码之后想要push到远程仓 ...
- Django基本知识
一.安装及使用 下载安装 命令行:pip3 install django==1.11.21 pycharm 创建项目 命令行: 找一个文件夹存放项目文件,打开终端: django-admin star ...