- 浏览: 185725 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
给定两颗二叉树,判断这两颗树是否相同,它们相同的条件是结构相同,对应节点的值也必须相同。同样采用递归的方式,先比较对应点的节点是否为空,如果都为空,那么在当前状态下返回true;如果一个空,一个不为空,返回false。如果都不为空的情况下,判断对应点的值,如果值不同返回false;如果值相同,递归判断当前节点的子树。代码如下:
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
给定两颗二叉树,判断这两颗树是否相同,它们相同的条件是结构相同,对应节点的值也必须相同。同样采用递归的方式,先比较对应点的节点是否为空,如果都为空,那么在当前状态下返回true;如果一个空,一个不为空,返回false。如果都不为空的情况下,判断对应点的值,如果值不同返回false;如果值相同,递归判断当前节点的子树。代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null) { return true; } else if(p != null && q != null) { if(p.val != q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } return false; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 271Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 275You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 392Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 380Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 506Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 573Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 484Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 674Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 478The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 437Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 587Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 596Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 432All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 909Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 936Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 609Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 705Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 870Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 796You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 736For a undirected graph with tre ...
相关推荐
本问题关注的是如何判断两个二叉树是否相同,即“Same Tree”问题。这个问题在面试和编程挑战中很常见,因为它涉及到对递归和树结构的理解。 首先,我们要明确什么是“相同”的定义。在这个场景下,两棵二叉树相同...
java面试算法/刷题
java java_leetcode-100-same-tree
python python_leetcode题解之100_Same_Tree
This is a small widget that provides quite configurable tree view list. It is based on ... Implementation follows best approach of Adapters from android so views at the same tree level are reusable.
js js_leetcode题解之100-same-tree.js
- **Same Tree**:判断两棵二叉树是否相同。 - **Balanced Binary Tree**:判断一个二叉树是否是平衡的。 - **Path Sum**:判断是否存在一条路径,其节点值之和等于给定的目标值。 - **Binary Tree Depth Order ...
4. **Same Tree**:判断两棵树是否结构相同且对应节点值相等。这涉及到深度优先搜索(DFS)或广度优先搜索(BFS)的实现。 5. **3Sum**:寻找数组中三个元素的和等于特定值的组合。可以使用双指针法解决,先排序...
- **相同的树(Same Tree)**: 判断两个二叉树是否相同。 - **平衡二叉树(Balanced Binary Tree)**: 判断一个二叉树是否是高度平衡的。 ##### 动态规划(Dynamic Programming) - **买卖股票的最佳时机(Best Time to ...
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
Same Tree、235. Lowest Common Ancestor of a Binary Search Tree等。通过解决这些题目,你可以加深对二叉树的理解,并提升解决问题的能力。 总结,Java中的二叉树题目不仅要求对数据结构有深入理解,还需要灵活...
https://leetcode.com/problems/same-tree/ Same Tree 101 https://leetcode.com/problems/symmetric-tree/ Symmetric Tree 102 https://leetcode.com/problems/binary-tree-level-order-traversal/ Binary Tree ...
Leetcode 使用 Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js...
#### 100.SameTree **题目描述:** 给定两棵二叉树 `p` 和 `q`,判断它们是否完全相同。 **解题思路:** 1. 需要处理两种特殊情况:两棵树均为空或者仅有一棵树为空。 2. 若两棵树均非空,则比较根节点的值,并...
与大家分享我学习算法的...数组/链表:树相关:AVLTree 平衡二叉搜索树BinaryHeap 二叉堆(优先队列)Num2TreeSum 数组树的和MaxDepth4Tree (leetcode 104)ValidBinarySearchTree (leetcode 98)SameTree (leetcode 100)...
Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate Reverse Polish Notation #169 Majority Element #171 Excel Sheet Column Number #217 ...
leetcode-js Leecode 经典题目 JavaScript TypeScript 题解...100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)
leetcode 2 和 c Leetcode_questions 目前拥有: 简单的: 1.二和(c) 7.反转整数(c) 9.回文数(c) ...100.Same Tree(c++) 101.对称树(c++) 104.二叉树的最大深度(c++) 108.将排序数组转换为二叉搜索树
sameTree: find_content_children: LeetCode 算法题 时间复杂度和空间复杂度权衡,时间复杂度的提升是以空间复杂度为代价的 仔细观察,LeetCode 上对每一次代码的提交的 执行时间 && 消耗内存 效率 = 算法效率 + ...