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.
二. 题目分析
题目的意思非常easy,推断两棵树是否同样,递归,对两棵树的结点进行比較就可以。
三. 演示样例代码
#include <iostream>
using namespace std;
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;
// p和q不同一时候到达叶节点,则剪枝
else if ((p != NULL && q == NULL) || (p == NULL && q != NULL))
return false;
return (p->val == q->val) && isSameTree(p->left, q->left)
&& isSameTree(p->right, q->right);
}
};
四. 小结
该题属于二叉树的遍历中的基础题目,难度不大。
leetcode笔记:Same Tree的更多相关文章
- leetCode笔记--binary tree
993. Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each dept ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
随机推荐
- 理解 Python 中的元类
本文编程环境:Jupyter NoteBook python3 类也是对象 在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.在 Python 中这一点仍然成立: class Objec ...
- 个人博客www.0x002.com
cnblogs只是相当于一个备份,感兴趣的读者赏脸关注下.
- React Native之网页组件WebView的使用与通信
在实际开发中,我们通常会嵌入一些html页面,官方为我们提供了一个非常好用的网页组件WebView,通过这个组件我们可以通过传入一个url或者是传入一段html 一. WebView的基本属性方法介绍 ...
- Java 中的 I/O
I/O 指的是 input 和 output ,也就是输入和输出,我们说的是 Java 中的 I/O,那我们就在站在虚拟机的角度去看看有哪些输入和输出.输入又可以称为数据源端,能想到的会有,文件,网络 ...
- 支撑大规模公有云的Kubernetes改进与优化 (3)
这一篇我们来讲网易为支撑大规模公有云对于Kubernetes的定制化. 一.总体架构 网易的Kubernetes集群是基于网易云IaaS平台OpenStack上面进行部署的,在外面封装了一个容器平台的 ...
- Python匹配中文的正则表达式
python 中的字符串: https://www.cnblogs.com/livingintruth/p/3282981.html # -*- coding: utf-8 -*- import re ...
- 学习Git操作的好资源
网上资源很多,极大的方便了我们学习新东西. 今天找到了几个简单明了的Git教程,用以备录共享. Learn Git Branching http://pcottle.github.io/learnG ...
- BZOJ 3572: [Hnoi2014]世界树 虚树 树形dp
https://www.lydsy.com/JudgeOnline/problem.php?id=3572 http://hzwer.com/6804.html 写的时候参考了hzwer的代码,不会写 ...
- SB!SB!SB! ----WriteUp
原题 下载图片 http://ctf5.shiyanbar.com/stega/ste.png 用Stegsolve查看 发现有个二维码 扫码可以知道flag
- Alpha 冲刺报告5
组长:吴晓晖 今天完成了哪些任务: 将服务端程序基本部署在阿里云上,还未进行测试 完成了手写记录的代码实现 处理团队问题 为明天的编程任务做准备 展示GitHub当日代码/文档签入记录: 明日计划: ...