[LeetCode] 100. Same Tree ☆(两个二叉树是否相同)

描述

解析

根与根比较,左右子树互相递归比较即可。

代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        } else if (p == null || q == null) {
            return false;
        } else if (p.val == q.val) {
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        } else {
            return false;
        }
    }
}

原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/10575673.html

时间: 2024-11-09 22:45:06

[LeetCode] 100. Same Tree ☆(两个二叉树是否相同)的相关文章

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

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

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

LeetCode 100. Same Tree 判断两棵二叉树是否相等

Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value. Example 1: Input: 1 1 / \ / 2 3 2 3 [1,2,3], [1,2,3] Output:

LeetCode 257. Binary Tree Paths (二叉树路径)

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 题目标签:Tree 这道题目给了我们一个二叉树,让我们记录所有的路径,返回一个array string list. 我们可以另外设一个fin

LeetCode 563. Binary Tree Tilt (二叉树的倾斜度)

Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0. The tilt of the 

[LeetCode] 100. Same Tree Java

题目: 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.. 题意及分析:给出两棵树,判断两棵树是否完全相同,结构和值都相同.有两种方法,一种是遍历树,对每次的节点做判断,比较复杂:另

Leetcode 94 Binary Tree Inorder Traversal 二叉树

二叉树的中序遍历,即左子树,根, 右子树 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 void dfs(vect