【LeetCode】100. Same Tree-相同树

一、描述:

二、思路:

属于二叉树类型,使用递归解决;

返回false:一棵树为空,且另一棵树不为空;

返回true两种情况:1两棵树均为空;2两棵树均不为空,且对应位置的结点完全相同;

递归调用,两树均为空是递归结束条件。

三、代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public boolean isSameTree(TreeNode p, TreeNode q) {
12         if(p==null&& q==null){
13             return true;
14         }
15         if(p==null&&q!=null || q!=null&&q==null){
16             return false;
17         }
18         if(p!=null&&q!=null){
19             return (p.val==q.val)&&isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
20         }
21         return false;
22     }
23 }
时间: 2024-10-10 00:08:50

【LeetCode】100. Same Tree-相同树的相关文章

leetCode 100. Same Tree 树

100. 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. 题目大意: 判断两个二叉树是否完全相同.包括他们节点的内容. 代码如下:(递归版) /**  * De

LeetCode 100 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. 分析 还

LeetCode 100. 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.  题目标题:Tree 这道题目给了我们两个二叉树,让我们判断这两个二叉树是否一摸一样.利用preOrder 来遍历tree, 对于每

[LeetCode] 100. Same Tree Java

题目: 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.. 题意及分析:给出两棵树,判断两棵树是否完全相同,结构和值都相同.有两种方法,一种是遍历树,对每次的节点做判断,比较复杂:另

leetCode 101. Symmetric Tree 对称树

101. 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]

小程序 - leetcode 100. Same Tree

https://leetcode.com/problems/same-tree/ /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ bool isSameTree(struct TreeNode* p, struct TreeNode* q) { if (p == NULL) retur

leetcode 之 Symmetric Tree 镜像树

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 递归解法:由于对称性,每次把一个节点的左子树和它兄弟节点的左子树进行

Leetcode 100. Same Tree

思路1: 递归,如果 p = q, 那么判段 sameTree(p.left)==sameTree(q.left) and sameTree(p.right) = sameTree(q.right) 1 class Solution(object): 2 def isSameTree(self, p, q): 3 """ 4 :type p: TreeNode 5 :type q: TreeNode 6 :rtype: bool 7 """ 8

leetcode 100. 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. 简单的递归. /** * Definition for a binary tree node. * struct TreeNode

Java [Leetcode 100]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. 解题思路: 递归调用比较左子树和右子树以及该节点. 代码描述: /** * Definition for a binar