`

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.

给定两颗二叉树,判断这两颗树是否相同,它们相同的条件是结构相同,对应节点的值也必须相同。同样采用递归的方式,先比较对应点的节点是否为空,如果都为空,那么在当前状态下返回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;
    }
}
0
0
分享到:
评论

相关推荐

    Code02_SameTree.java

    java面试算法/刷题

    Same-Tree.rar_Same Same

    Given two binary trees, write a function to check if they are equal or not. 判断两个二叉树是否相同

    Tree Listview

    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.

    cpp-算法精粹

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

    LeetCode最全代码

    * [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]...

    lrucacheleetcode-luoleet:LeetcodesolutionsbyXinhangLuoandQinghaoDai

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

    algorithms:只是个菜鸟。这个项目是在空闲时间编写的。我会不定期更新。希望与您分享一些东西〜

    与大家分享我学习算法的...数组/链表:树相关:AVLTree 平衡二叉搜索树BinaryHeap 二叉堆(优先队列)Num2TreeSum 数组树的和MaxDepth4Tree (leetcode 104)ValidBinarySearchTree (leetcode 98)SameTree (leetcode 100)...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    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:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

    leetcode-js Leecode 经典题目 JavaScript TypeScript 题解...100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)

    leetcode2sumc-Leetcode_questions:Leetcode_questions

    leetcode 2 和 c Leetcode_questions 目前拥有: 简单的: 1.二和(c) 7.反转整数(c) 9.回文数(c) ...100.Same Tree(c++) 101.对称树(c++) 104.二叉树的最大深度(c++) 108.将排序数组转换为二叉搜索树

    leetcode分发糖果-LeetCodeAlgorithm:学习算法,LeetCode刷题,建议经典书籍《算法导论》《算法4》,使用C++和

    sameTree: find_content_children: LeetCode 算法题 时间复杂度和空间复杂度权衡,时间复杂度的提升是以空间复杂度为代价的 仔细观察,LeetCode 上对每一次代码的提交的 执行时间 && 消耗内存 效率 = 算法效率 + ...

    leetcode刷多少面试-LeetCode:力码

    删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者...

    leetcode答案-leetcode:leetcode

    leetcode 答案 leetcode 08/18 Unique Paths 应该是简单的数学排列组合问题,提炼一下其实就一句话:...Same Tree 这种简单的二叉树遍历,丝毫没有难度啊。。 Insertion Sort List 在这里遇到前所未遇的惨败——提交了

    leetcode2sumc-coding:用C++编码

    同一棵树:#[./LeetCode/100_Same Tree.cpp] 序列化和反序列化二叉树:# 将根数与叶数相加:# 二叉搜索树 最低共同祖先:1); 2) 红黑树 AVL树 展开树 段树RMQ:范围最小查询 DFS:由,由 (单一来源) (所有对) ...

    leetcode叫数-Leetcode_JS:leetcode编程题,javascript版本

    Same Tree 这道题是关于二叉树的题,非常基础的题,递归思路。这道题主要就是熟悉二叉树的相关操作,这里给出了一个前序输入二叉树的函数。 ##NO.112 Path Sum 这道题是关于二叉树的题,递归思路,也就是DFS的思路。...

    The Log-Structured Merge-Tree

    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的Java代码

    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-Chinese-Translation-Voting:MMORPG Tree of Savior - 中文翻译众包系统

    救世主之树国际繁中版群众翻译系统Tree of Savior - Crowdsourcing Translation程式流程Workflow原文资料抓取(Base data parsing) zh :系统自动至官方GitHub下载当前的最新语言文件包。 en : Parsing the .tsv ...

    swstp 快速生成树协议

    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.

Global site tag (gtag.js) - Google Analytics