Loading [MathJax]/jax/input/TeX/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Same Tree

Same Tree

作者头像
发布于 2018-09-03 08:43:27
发布于 2018-09-03 08:43:27
37700
代码可运行
举报
文章被收录于专栏: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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode笔记:100. Same Tree
这个思路还比较直接,考虑全面一点就好了。首先考虑节点为空的情况,如果两个都为空,那么直接相等;如果一个为空一个不为空,那么不相等;如果两个都不为空,那么继续进行深层次的判断。 首先看两个节点的值是否相等,不相等则二叉树不等,然后判断其子节点,这时候使用递归就可以了,对两个节点的左节点和右节点分别调用这个函数,只有都返回相等时,才表示两个节点完全相同,由于递归,其子节点也就一层层地判断下去了,整个二叉树就会遍历完成。
Cloudox
2021/11/23
1670
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
4890
​画解算法:100. 相同的树
https://leetcode-cn.com/problems/same-tree/
灵魂画师牧码
2019/07/11
4130
​画解算法: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
1720
leetcode树之相同的树
这里采用递归的思路,当p及q都为null返回true;若p和q都不为null且p.val等于q.val那么则递归判断isSameTree(p.left,q.left)及isSameTree(p.right,q.right);其他情况返回false。
code4it
2020/09/24
6210
leetcode树之相同的树
【leetcode刷题】T113-相同的树
https://leetcode-cn.com/problems/same-tree/
木又AI帮
2019/07/18
3870
【算法】TOP101-二叉树篇(持续更新ing)
解题思路: 由题目可知,这是一颗二叉搜索树.二叉搜索树的特点就是他的中序遍历是有序的.所以本题我们大的框架就是要在中序遍历里完成.具体解题如下:
xxxflower
2023/10/23
1520
【算法】TOP101-二叉树篇(持续更新ing)
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
5290
Tree - 100. Same Tree
Given two binary trees, write a function to check if they are the same or not.
ppxai
2020/09/23
5250
【leetcode刷题】20T51-相同的树
https://leetcode-cn.com/problems/same-tree
木又AI帮
2020/07/16
2980
【Leetcode】100. 相同的树
大多数的二叉树题目都是用递归可以解的。 所以当拿到二叉树的题目的时候,我们首先就是看看能拆解成哪些子问题。 这个问题的子问题很简单,就是左子树,右子树都相等的二叉树是相同的二叉树。
Leetcode名企之路
2019/03/08
4380
leetcode-100. 相同的树
  这道题可以采用递归判断的方法。首先对对传进来的的两个节点进行判空,若两个均为空则说明是一样的,但是任一节点一个为空另一个非空的和两节点都有值,但是值不同的均为不同的树了。若两节点有值且相同,则继续往下遍历,对应的左子节点与右子节点均要相同,递归直到结束,若都相同则为相同的树,否则不是相同的树。
灰太狼学Java
2022/06/17
1610
leetcode-100. 相同的树
LeetCode 100. 相同的树
https://leetcode-cn.com/problems/same-tree/
freesan44
2021/09/23
3050
LeetCode 100. 相同的树
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
5530
Tree - 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
ppxai
2020/09/23
7930
知识改变命运 数据结构【二叉树OJ题】
用户11319080
2024/10/17
730
知识改变命运 数据结构【二叉树OJ题】
相同的树、对称二叉树、翻转二叉树
JavaScript实现LeetCode第100题:相同的树 JavaScript实现LeetCode第101题:对称二叉树 JavaScript实现LeetCode第226题:翻转二叉树 这几道题其实很相似,所以可以放在一起理解。 相同的树 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同。 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 示例 1: 输入: 1 1 / \ / \ 2 3
木子星兮
2020/07/17
4840
相同的树、对称二叉树、翻转二叉树
LeetCode 100 及 101题
输入: 1 1 / \ 2 2 [1,2], [1,null,2] 输出: false
Carlos Ouyang
2019/08/19
4170
LeetCode 100 及 101题
100. 相同的树
一 .题目: 二 .思路: 递归保证每个结点都相同即可 三. 代码: class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null&&q==null){ return true; } if ((p==null&&q!=null)||(p!=null&&q==null)){ return fal
名字是乱打的
2022/05/13
4010
100. 相同的树
leetcode-101. 对称二叉树
运用递归,通过 同步移动 两个指针的方法来遍历这棵树,p 指针和 q 指针一开始都指向这棵树的根,随后 p 右移时,q 左移,p 左移时,q 右移。每次检查当前 p 和 q 节点的值是否相等,如果相等再判断左右子树是否对称。
灰太狼学Java
2022/06/17
1680
leetcode-101. 对称二叉树
相关推荐
LeetCode笔记:100. Same Tree
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档