LeetCode刷题之三:判断两个二叉树是否相同

题目为:

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.

解题思路:这种题目也是递归操作简单

代码为:

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

    }
}
时间: 2024-08-30 08:39:31

LeetCode刷题之三:判断两个二叉树是否相同的相关文章

Leetcode刷题记录:构建最大数二叉树

题目要求,题目地址 给定一个不含重复数字的数组,最大二叉树构建规则如下: 1.根是数组中最大的数字 2.左边的子树是最大数字左边的内容 3.右边的子树是最大数字右边的内容 答案 class Solution(object): def constructMaximumBinaryTree(self, nums): """ :type nums: List[int] :rtype: TreeNode """ #print(max(nums)) #pr

LeetCode 刷题之二:寻找二叉树的最大深度

题目为: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 阶梯思路:对于这种题目最简单的方法就是递归操作了 代码为: /** * Definition for binary tree * public class TreeNod

LeetCode刷题之一:寻找只出现一次的数字

投简历的时候看到了个刷题网站,http://www.nowcoder.com/527604,就做了一套题,现记录下来. 题目为: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it withou

Leetcode刷题记录:编码并解码短网址

题目要求 编写一个类,提供两个方法.一个可以将普通的网址编码成短网址,一个可以将短网址还原为普通网址. 参考题解 # 使用随机函数,生成短网址,保存在dict中,避免重复 import random import math import re class Codec: charbase = [x for x in "0123456789abcdefghijklmnopqrstuvwxyz"] urldict = {} longurldict = {} def get_random_ch

C++将二叉树转为双向链表及判断两个链表是否相交_C 语言

把二叉查找树转变成排序的双向链表例如: 转换成双向链表 4=6=8=10=12=14=16 struct BSTreeNode { int m_nValue; // value of node BSTreeNode *m_pLeft; // left child of node BSTreeNode *m_pRight; // right child of node }; 首先阐述下二叉排序树: 它首先要是一棵二元树,在这基础上它或者是一棵空树:或者是具有下列性质的二元树: (1)若左子树不空,

[LeetCode] Average of Levels in Binary Tree 二叉树的层平均值

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and o

[LeetCode] Judge Route Circle 判断路线绕圈

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. The move sequence is represented by a string. And each move is represent by a characte

编程-Java 三目运算符 判断两个对象是否为空

问题描述 Java 三目运算符 判断两个对象是否为空 //住院非空,对住院进行处理,住院不为空,判断门诊是否为非空,对门诊进行处理 //zyyzjymx!=null?f1(zyyzjymx):mzzdjymx!=null?f1(mzzdjymx):null; 不会写代码... 思路如上 求大神解决~ 解决方案 zyyzjymx!=null?(f1(zyyzjymx)):(mzzdjymx!=null?f1(mzzdjymx):null); 解决方案二: 你的条件写错了吧?住院非空和住院不为空 不

bug-这是我写的表达式求值,在编译器中运行是对的,但在刷题系统中却说是错,求打什么呢帮我找找Bug

问题描述 这是我写的表达式求值,在编译器中运行是对的,但在刷题系统中却说是错,求打什么呢帮我找找Bug 2C #include""stdio.h""#include""stdlib.h""#include""malloc.h""#include""string.h""#include""math.h""#de