首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Same Tree

Same Tree

作者头像
发布于 2018-09-03 08:43:27
发布于 2018-09-03 08:43:27
38500
代码可运行
举报
文章被收录于专栏:WD学习记录WD学习记录
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public 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!=null&&q==null)
        return false;
        if(p.val==q.val){
            return IsSameTree(p.left,q.left)&&IsSameTree(p.right,q.right);
        }else{
            return false;
        }
        
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年06月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
​画解算法:100. 相同的树
https://leetcode-cn.com/problems/same-tree/
灵魂画师牧码
2019/07/11
4180
​画解算法:100. 相同的树
LeetCode 100. Same Tree (Tag : DFS)
Given two binary trees, write a function to check if they are the same or not.
用户7447819
2021/07/23
1780
【Leetcode】100. 相同的树
大多数的二叉树题目都是用递归可以解的。 所以当拿到二叉树的题目的时候,我们首先就是看看能拆解成哪些子问题。 这个问题的子问题很简单,就是左子树,右子树都相等的二叉树是相同的二叉树。
Leetcode名企之路
2019/03/08
4460
leetcode-100. 相同的树
  这道题可以采用递归判断的方法。首先对对传进来的的两个节点进行判空,若两个均为空则说明是一样的,但是任一节点一个为空另一个非空的和两节点都有值,但是值不同的均为不同的树了。若两节点有值且相同,则继续往下遍历,对应的左子节点与右子节点均要相同,递归直到结束,若都相同则为相同的树,否则不是相同的树。
灰太狼学Java
2022/06/17
1680
leetcode-100. 相同的树
leetcode-101. 对称二叉树
运用递归,通过 同步移动 两个指针的方法来遍历这棵树,p 指针和 q 指针一开始都指向这棵树的根,随后 p 右移时,q 左移,p 左移时,q 右移。每次检查当前 p 和 q 节点的值是否相等,如果相等再判断左右子树是否对称。
灰太狼学Java
2022/06/17
1760
leetcode-101. 对称二叉树
leetcode: 100. Same Tree
Problem # 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
JNingWei
2018/09/27
3720
一天一大 lee(相同的树)难度:简单-Day20200807
题目: 给定两个二叉树,编写一个函数来检验它们是否相同。 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 示例 示例 1 输入: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] 输出: true 示例 2 输入: 1 1 / \ 2 2
前端小书童
2020/09/24
3210
一天一大 lee(相同的树)难度:简单-Day20200807
Leetcode Golang 100. Same Tree.go
版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/88935350
anakinsun
2019/04/12
3990
LeetCode笔记:100. Same Tree
这个思路还比较直接,考虑全面一点就好了。首先考虑节点为空的情况,如果两个都为空,那么直接相等;如果一个为空一个不为空,那么不相等;如果两个都不为空,那么继续进行深层次的判断。 首先看两个节点的值是否相等,不相等则二叉树不等,然后判断其子节点,这时候使用递归就可以了,对两个节点的左节点和右节点分别调用这个函数,只有都返回相等时,才表示两个节点完全相同,由于递归,其子节点也就一层层地判断下去了,整个二叉树就会遍历完成。
Cloudox
2021/11/23
1760
Q101 Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 /
echobingo
2018/04/25
5630
【leetcode刷题】T113-相同的树
https://leetcode-cn.com/problems/same-tree/
木又AI帮
2019/07/18
3920
【leetcode刷题】20T51-相同的树
https://leetcode-cn.com/problems/same-tree
木又AI帮
2020/07/16
3050
100 Same Tree
/** *题意:判断两棵二叉树 * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} p * @param {TreeNode} q * @return {boolean} */ var isSameTree = function(p
用户1624346
2018/04/18
5330
【算法】TOP101-二叉树篇(持续更新ing)
解题思路: 由题目可知,这是一颗二叉搜索树.二叉搜索树的特点就是他的中序遍历是有序的.所以本题我们大的框架就是要在中序遍历里完成.具体解题如下:
xxxflower
2023/10/23
1590
【算法】TOP101-二叉树篇(持续更新ing)
【LeetCode - 101】平衡二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
周三不加班
2019/09/03
4110
【LeetCode - 101】平衡二叉树
【2025-03-02】基础算法:二叉树 相同 对称 平衡 右视图
📝前言说明: ●本专栏主要记录本人的基础算法学习以及LeetCode刷题记录,主要跟随B站博主灵茶山的视频进行学习,专栏中的每一篇文章对应B站博主灵茶山的一个视频 ●题目主要为B站视频内涉及的题目以及B站视频中提到的“课后作业”。 ●文章中的理解仅为个人理解。 ●文章中的截图来源于B站博主灵茶山,如有侵权请告知。
用户11029137
2025/03/03
700
【2025-03-02】基础算法:二叉树 相同 对称 平衡 右视图
Q100 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 / \ / \
echobingo
2018/04/25
4940
相同的树、对称二叉树、翻转二叉树
JavaScript实现LeetCode第100题:相同的树 JavaScript实现LeetCode第101题:对称二叉树 JavaScript实现LeetCode第226题:翻转二叉树 这几道题其实很相似,所以可以放在一起理解。 相同的树 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同。 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 示例 1: 输入: 1 1 / \ / \ 2 3
木子星兮
2020/07/17
4980
相同的树、对称二叉树、翻转二叉树
LeetCode 100 及 101题
输入: 1 1 / \ 2 2 [1,2], [1,null,2] 输出: false
Carlos Ouyang
2019/08/19
4190
LeetCode 100 及 101题
Tree - 100. Same Tree
Given two binary trees, write a function to check if they are the same or not.
ppxai
2020/09/23
5430
相关推荐
​画解算法:100. 相同的树
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档