就是判断两棵树的值和结构是否相同

注意:要判断是否所有的树节点是否为NULL

 /**
* 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 && !q) {
return true;
}
else if(!p||!q){
return false;
}
else{
if(p->val != q->val) return false;
else return isSameTree(p->left, q->left) &&
isSameTree(p->right, q->right);
}
}
};

Leetcode 100 Same Tree 二叉树的更多相关文章

  1. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  2. 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 ...

  3. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  4. (二叉树 递归 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 ...

  5. [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)

    描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...

  6. 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 ...

  7. LeetCode 100. Same Tree (相同的树)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  8. [LeetCode] 100. Same Tree 相同树

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  9. leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

随机推荐

  1. js cookie创建读取删除函数封装

    js cookie创建读取删除函数封装 一.总结 都是为了方便操作,这样弄了很方便 1.创建cookie的函数封装的作用:方便设置过期时间expire,方便设置键和值 2.查询cookie的数据封装的 ...

  2. [TypeStyle] Use fallback values in TypeStyle for better browser support

    You can increase the browser support of your CSS using fallback values and vendor prefixes. This les ...

  3. 【C++竞赛 E】xxx和yyy的旅行

    时间限制:1s 内存限制:32MB 问题描述 有n个城市和m条双向铁路.对于任意两个不同的城市x和城市y,两个城市之间有双向铁路,否则有双向公路,通过任意一条直达公(铁)路花费一小时.城市x与城市y存 ...

  4. java并发api总结

    开发十年,就只剩下这套架构体系了! >>>   1.java.util.concurrent包 1.1 Executors Executor:接口,仅有一个方法为execute(Ru ...

  5. [RxJS] Split an RxJS Observable into groups with groupBy

    groupBy() is another RxJS operator to create higher order observables. In this lesson we will learn ...

  6. js获取浏览器尺寸

    Javascript: alert(document.body.clientWidth);        //网页可见区域宽(body) alert(document.body.clientHeigh ...

  7. 【60.97%】【BZOJ 1925】 [Sdoi2010]地精部落

    Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 1194  Solved: 728 [Submit][Status][Discuss] Descript ...

  8. 【22.17%】【codeforces718B】 Efim and Strange Grade

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  9. c# 安全队列

    using System;using System.Collections.Concurrent;using System.Collections.Generic;using System.Linq; ...

  10. PatentTips - Hardware virtualization such as separation kernel hypervisors

    BACKGROUND 1. Field Innovations herein pertain to computer virtualization, computer security and/or ...