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         if not p and not q:
 9             return True
10
11         if p and q:
12             if p.val == q.val:
13                 return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
14             else:
15                 return False
16
17         return False

思路二: 如果两个树相同,说明按照一定的方式遍历这两个树,应该得到一样的结果。这里用的方法是使用stack来实现先序遍历(pre-order)。注意pre-order遍历时是,root -> root.left -> root.right。所以push进stack的时候应该先push root.right再push root.left。

 1 class Solution(object):
 2     def isSameTree(self, p, q):
 3         """
 4         :type p: TreeNode
 5         :type q: TreeNode
 6         :rtype: bool
 7         """
 8         if p == None and q == None:
 9             return True
10
11         stack = [(p, q)]
12
13         while stack:
14             l, r = stack.pop()
15
16             if l == None and r == None:
17                 continue
18             elif l and r and l.val == r.val:
19
20                 stack.append((l.right, r.right))
21
22                 stack.append((l.left, r.left))
23             else:
24                 return False
25
26         return True
时间: 2024-12-17 13:39:43

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

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] 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 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(递归)

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

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 ☆(两个二叉树是否相同)

描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * 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) { i

[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. 递归遍历 一刷: public boolean isSameTree(TreeNode p, TreeNode q) { if ((