- 浏览: 173388 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 226You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 342Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 333Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 451Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 511Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 430Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 619Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 428The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 388Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 517Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 531Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 861Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 555Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 764Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 735You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 629For a undirected graph with tre ...
相关推荐
java面试算法/刷题
Given two binary trees, write a function to check if they are equal or not. 判断两个二叉树是否相同
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.
Same Tree Symmetric Tree Balanced Binary Tree Flatten Binary Tree to Linked List Populating Next Right Pointers in Each Node II 二叉树的构建 Construct Binary Tree from Preorder and Inorder Traversal ...
* [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]...
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 ...
与大家分享我学习算法的...数组/链表:树相关: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 上对每一次代码的提交的 执行时间 && 消耗内存 效率 = 算法效率 + ...
删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者...
leetcode 答案 leetcode 08/18 Unique Paths 应该是简单的数学排列组合问题,提炼一下其实就一句话:...Same Tree 这种简单的二叉树遍历,丝毫没有难度啊。。 Insertion Sort List 在这里遇到前所未遇的惨败——提交了
同一棵树:#[./LeetCode/100_Same Tree.cpp] 序列化和反序列化二叉树:# 将根数与叶数相加:# 二叉搜索树 最低共同祖先:1); 2) 红黑树 AVL树 展开树 段树RMQ:范围最小查询 DFS:由,由 (单一来源) (所有对) ...
Same Tree 这道题是关于二叉树的题,非常基础的题,递归思路。这道题主要就是熟悉二叉树的相关操作,这里给出了一个前序输入二叉树的函数。 ##NO.112 Path Sum 这道题是关于二叉树的题,递归思路,也就是DFS的思路。...
High-performance transaction system applications typically insert rows in a History table to provide an ... at the same time the transaction system generates log records for purposes of system recovery.
leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ com.leetcode.array Search a 2D Matrix Spiral Matrix com.leetcode.list ...com.leetcode.tree ...Tree ...Tree Same Tree
救世主之树国际繁中版群众翻译系统Tree of Savior - Crowdsourcing Translation程式流程Workflow原文资料抓取(Base data parsing) zh :系统自动至官方GitHub下载当前的最新语言文件包。 en : Parsing the .tsv ...
to the same spanning-tree instance, see Chapter 19, “Configuring MSTP.” For information about other spanning-tree features such as Port Fast, UplinkFast, root guard, and so forth, see Chapter 20, ...
练习:等价二叉查找树 实现 Walk 函数。 测试 Walk 函数。 函数 tree.New(k) 用于构造一个随机结构的已排序二叉查找树,它保存了值 k, 2k...Same(tree.New(1), tree.New(1)) 应当返回 true,而 Same(tree.New(1), tree.