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.

大意:

给出两个二叉树,写一个函数来检查两者是否相等。

所谓相等,是指他们结构相同且节点有同样的值。

思路:

这个思路还比较直接,考虑全面一点就好了。首先考虑节点为空的情况,如果两个都为空,那么直接相等;如果一个为空一个不为空,那么不相等;如果两个都不为空,那么继续进行深层次的判断。

首先看两个节点的值是否相等,不相等则二叉树不等,然后判断其子节点,这时候使用递归就可以了,对两个节点的左节点和右节点分别调用这个函数,只有都返回相等时,才表示两个节点完全相同,由于递归,其子节点也就一层层地判断下去了,整个二叉树就会遍历完成。

代码(Java):

/**
 * 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;

            if (isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) return true;
            else return false;
        } else {
            return false;
        }
    }
}

其实还可以进一步精简代码,可以看下Discuss最火的代码,思路是一致的,只是精简到了极致,确实很赞:

精简代码(Java):

/**
 * 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;
        if(p == null || q == null) return false;
        if(p.val == q.val)
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        return false;
    }
}

版权所有:http://blog.csdn.net/cloudox_

时间: 2024-10-21 22:30:07

LeetCode笔记:100. Same Tree的相关文章

[LeetCode] NO. 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. [题目解析]  二叉树的问题,转化成当前节点与左子树,右子树问题.注意一些临界条件的判断. /** * Definitio

LeetCode OJ 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. 很简单,直接上代码: 1 /** 2 * Definition for a binary tree node. 3 * public

leetcode笔记:Binary Tree Level Order Traversal

一. 题目描述 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 二. 题目分析 无. 三. 示例代码 参考了Discussion中stellari的做法,递归进行层次遍历,并将每个

【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. Solution 1:递归,关键在于找全return true.false的所有条件 1 /** 2 * Definition fo

leetcode笔记:Binary Tree Level Order Traversal II

一. 题目描述 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 二. 题目分析 由于使用了vector,这一题只需Binar

【LeetCode】100. Same Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51541570 Subject 出处:https://leetcode.com/problems/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 structur

leetcode笔记: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. 二. 题目分析 题目的意思很简单,判断两棵树是否相同,递归,对两棵树的结点进行比较即可. 三. 示例代码 #incl

<LeetCode OJ> 100. Same Tree

100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy 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 t

【leetcode刷题笔记】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order traver

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