Leetcode 100. 相同的树(待整理)

1.题目描述

给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:

输入:       1         1
          / \       /          2   3     2   3

        [1,2,3],   [1,2,3]

输出: true

示例 2:

输入:      1          1
          /                    2             2

        [1,2],     [1,null,2]

输出: false

示例 3:

输入:       1         1
          / \       /          2   1     1   2

        [1,2,1],   [1,1,2]

输出: false

  

2.解法一:递归

3.解法二:非递归

4.问题转化:树的序列化,比较字符串



原文地址:https://www.cnblogs.com/paulprayer/p/10145065.html

时间: 2024-10-18 17:40:14

Leetcode 100. 相同的树(待整理)的相关文章

[leetcode] 100. 相同的树

100. 相同的树 判断两颗二叉树是否完全相同,把该树转变为数组展示形式, 前序遍历,然后看结果是否一致即可 class Solution { String s = ""; void dfs(TreeNode tree){ if (tree == null) return; s+=tree.val; if (tree.left!=null) dfs(tree.left); if (tree.right!=null) dfs(tree.right); } public boolean i

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.相同的树

class Solution: def isSameTree(self, p, q) -> bool: if not p and not q: return True elif p is not None and q is not None: if p.val==q.val: return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right) else: return False else: return Fals

LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40

572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验?s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 每日一算法2019/6/12Day 40LeetCode572. Subtree of Another Tree 示例 1: 给定的树 s: 3 / 4 5 / 1 2 给定的树 t: 4 / 1 2 返回 true,因为 t

[LeetCode] Scramble String(树的问题最易用递归)

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

100. 相同的树

100. 相同的树 https://leetcode-cn.com/problems/same-tree/description/ package com.test; /** * @Author stono * @Date 2018/8/27 上午8:59 */ public class Lesson100 { public static void main(String[] args) { TreeNode t1 = new TreeNode(1); TreeNode t2 = new Tre

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

[C++]LeetCode: 100 Convert Sorted Array to Binary Search Tree (AVL树)

题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路:给出一个排序的数组,如何构造一个平衡二叉查找树?平衡二叉查找树要求任一结点的左右子树的高度差不能超过一,也叫做高度平衡树.如果让我们从一个排序数组中选取一个元素做树的根,我们会选择哪一个树呢?凭直觉,我们觉得应该选择数组的中间值做根,事实也是这样,平衡二叉查找树(AVL树)的根应该是排序数组

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, 对于每