代码编织梦想

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.

Example 1:
Given tree s:

     3
    / \
   4   5
  / \
 1   2

Given tree t:

   4 
  / \
 1   2

Return true, because t has the same structure and node values with a subtree of s.

给出两棵二叉树s和t, 判断t是不是s的子树。

思路:
用到了相同树的判断,如果不是相同的树,就进一步判断t 在s的左子树,右子树中有没有相同的树

    public boolean isSubtree(TreeNode s, TreeNode t) {
        if(s == null) return false;
        if(isSame(s, t)) return true;
        return isSubtree(s.left, t) || isSubtree(s.right, t);
    }
    
    boolean isSame(TreeNode s, TreeNode t) {
        if(s == null && t == null) return true;
        if(s == null || t == null) return false;
        if(s.val == t.val) return isSame(s.left, t.left) && isSame(s.right, t.right);
        return false;
    }

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/level_code/article/details/111036261

二维数组的螺旋遍历-爱代码爱编程

原理及遍历结果如图所示:示例代码private static List<Integer> spiralArray(int[][] array) { // 数组为空,返回null if (array.length == 0 || array[0].length == 0) { return

快速排序-爱代码爱编程

#include <iostream> using namespace std; int Partition(int a[], int left, int right) { int i = left, j = right + 1; int k = a[left]; while(1) { while(a[++i] < k

【C语言】——残缺棋盘问题-爱代码爱编程

1.题目描述:残缺棋盘问题 2.解题分析: 利用分治算法将棋盘细化,逐步解决。首先判断残缺的位置在哪里,然后在中间填上对应的三格板,然后通过中间将其分成了小残缺棋盘,从而问题得以解决     3.代码实现: #include <stdio.h> #include <math.h> void TileBoard(int tr,

Python笔记之hashlib加密!!!-爱代码爱编程

文章目录 1.加密算法的介绍1.1Hash1.2MD5算法1.2.1什么是MD5算法?1.2.2MD5功能1.2.3MD5算法的特点1.2.4MD5算法是否可逆?1.2.5MD5用途1.3SHA-12.MD5与SHA-1的比较3.Python中关于算法的一些例子。 1.加密算法的介绍 关于加密算法的小故事: 在我们上网的时候会注册账号,密码等

Java从零开始 第5讲 数组,数组算法-爱代码爱编程

Java从零开始 第5讲 数组,数组算法 什么是数组声明一个数组数组基本操作数组操作常见问题数组的两种常用简单算法冒泡排序二分查找转载请注明出处 什么是数组 数组是相同数据类型的多个数据的集合,放在数组中的元素都为相同的数据类型,且他们按线性顺序排列,即一个一个的排列。 声明一个数组 声明数组主要有四种格式: 数据类型[] 数组名称 =

2020-12-11-爱代码爱编程

#include<stdio.h> float average(float ar[ ],int n) { int i; float sum = 0; for ( i = 0; i < n; i++) {sum = sum + ar[i]; printf("%f\n",ar[i]);

leetcode:n对括号的组合-爱代码爱编程

Given n pairs of parentheses,write a function to generate are all combinations of well-formed parentheses. Example 1: Input:n=3 Output: ["((()))","(()())","(())()","()(())","()()

leetcode540 有序数组中的单一元素-爱代码爱编程

题目描述 题目链接 代码 public int singleNonDuplicate(int[] nums) { int left = 0; int right = nums.length - 1; while (left <= right) { int mid =

iOS LeetCode☞合并两个有序链表-爱代码爱编程

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 代码如下 func mergeTwoLists(_ l1: ListNode?, _ l2: ListNo

605. 种花问题 ( 贪心 )-爱代码爱编程

LeetCode: 605. 种花问题 贪心题 官方题解思路是 : 从左到右遍历,遇到一个 0,判断其左右是否为0, 是 0 的话, count++ 当 count >= n 返回 true 我的思路: 遍历数组 不能相邻种植,即只可以种偶数位, 只可以种奇数位。 分别统计偶数位、奇数位可以种的数量,当 odd >= n 或

[算法练习及思路-leetcode每日一题(Java解法)]No300.最长上升子序列-爱代码爱编程

题号:no300 题目名:最长上升子序列 原题URL:https://leetcode-cn.com/problems/longest-increasing-subsequence/ 题目描述 给定一个无序的整数数组,找到其中最长上升子序列的长度。 示例 示例 1: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最

35. 搜索插入位置(python3)-爱代码爱编程

题目:https://leetcode-cn.com/problems/search-insert-position/ 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 你可以假设数组中无重复元素。 示例 1: 输入: [1,3,5,6], 5 输出: 2 示例 2: 输入: