[LC] 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.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
} else if (p == null && q != null || 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);
}
}
[LC] 100. Same Tree的更多相关文章
- 100. Same Tree(C++)
100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...
- LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number
100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...
- 100.Same Tree(E)
100. same tree 100. Same Tree Given two binary trees, write a function to check if they are the same ...
- <LeetCode OJ> 100. Same Tree
100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...
- 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 ...
- 100. Same Tree同样的树
[抄题]: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- LeetCode之100. Same Tree
------------------------------------------ 递归比较即可 AC代码: /** * Definition for a binary tree node. * p ...
随机推荐
- Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之数字类型(number)
Python 数字类型(number)用于存储数值.数据类型是不允许改变的,这就意味着如果改变number数据类型的值,将重新分配内存空间. 1. 一个简单的示例: # 以下实例在变量赋值时数字类 ...
- C#-类型转换和引用转换
对象的引用可以被: 隐式地向上转换 显示的向下转换 向上转换 向上转换是指一个从一个基类指向一个子类: House house = new House(); Asset asset = house; ...
- 阿里云服务器下安装配置phpMyAdmin
1.下载phpMyAdmin wget http://www.phpmyadmin.net/home_page/downloads.php 2.解压下载的文件 tar -zvxf phpMyAdmin ...
- C#判断两个字符串是否相等的方法 ,还有char赋空值办法。
string str1="Test"; string str2 = "Test"; if (str1==str2) //第一种判断方式 { //第二种判断方式 ...
- thrift生成c++服务端和客户端
https://blog.csdn.net/jdx0909/article/details/84727523 https://blog.csdn.net/luoyexuge/article/detai ...
- python-day3爬虫基础之下载网页
今天主要学习了关于网页下载器的一些内容,下边做一下总结: 1.网页下载器,顾名思义,就是将URL所对应的网页以HTML的形式下载到本地,最终存储成本地文件或者还是本地内存字符串,然后进行后续的分析与处 ...
- GetCharWidth32
#include <windows.h> #include<stdio.h> // 窗口函数的函数原形 LRESULT CALLBACK MainWndProc(HWND, U ...
- Vue 项目中应用
Vue使用 一.vue生命周期 # main.js import Vue from 'vue' import App from './App.vue' import router from './ro ...
- 记录一次追踪@AutoWired的过程
目录 记录一次追踪@AutoWired的过程 前言 疑惑:依赖究竟是怎么自动注入的 AutoWiredAnnotationBeanPostProcessor中探究 自动注入debug流程追踪 dete ...
- MACOSX下查看某个端口被哪个程序占用及杀进程方法
sudo lsof -i :9000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 61342 a 313u IPv6 0x11111 ...