当前位置:网站首页>有关二叉树的一些练习题
有关二叉树的一些练习题
2022-06-27 08:48:00 【牧..】
文章目录
二叉树的第三部分
这一部分 是 有关一些 二叉树 的 练习题
相同的树

附上代码
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p!= null && q == null || p == null && q != null) {
return false;
}
if(p == null && q == null ) {
return true;
}
if(p.val != q.val) {
return false;
}
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
}
时间复杂度
在来一个 小问题 这 函数 的时间复杂度 为 多少
这里我们都要遍历两颗树的结点 ,这里 需要看一下p和 q 的每棵树的结点个数
这里求 p 的 结点M 和 q 的 结点树N 的最小值
0(min(M,N))
看完了相同的 树那么接下来 来看 一道类似的题目
另一棵树的子树


附上代码
class Solution {
private boolean isSamTree(TreeNode p,TreeNode q) {
if(p == null && q != null || p != null && q == null) {
return false;
}
if(p == null && q == null) {
return true;
}
if(p.val != q.val) {
return false;
}
return isSamTree(p.left,q.left) && isSamTree(p.right,q.right);
}
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if(root == null || subRoot == null) {
return false;
}
// 根结点和 subroot是不是两颗相同的树
if(isSamTree(root,subRoot)) {
return true;
}
//subRoot 是不是 root 的左子树
if(isSubtree(root.left,subRoot)) {
return true;
}
if(isSubtree(root.right,subRoot)) {
return true;
}
return false;
}
}
时间复杂度
这里 需要 root 的每个结点 与 subRoot 比较 所以 如果 root 有 S 个结点 subRoot 有 T 个结点,每个 S都需要去 T 比较
所以时间复杂度 就为0(S * N)
平衡二叉树

附上代码
class Solution {
public int height(TreeNode root) {
if(root == null) {
return 0;
}
int leftHeight = height(root.left);
int rightHeight = height(root.right);
return Math.max((leftHeight+1),(rightHeight+1));
}
// 判断连个树是否为 平衡二叉树
public boolean isBalanced(TreeNode root) {
if(root == null) {
return true;
}
int left = height(root.left);
int right = height(root.right);
return Math.abs(left - right) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
}
优化后
class Solution {
public int height(TreeNode root) {
if(root == null) {
return 0;
}
int leftHeight = height(root.left);
int rightHeight = height(root.right);
if(leftHeight >=0 && rightHeight >=0 && Math.abs(leftHeight - rightHeight) <=1) {
return Math.max(leftHeight,rightHeight) + 1;
}else {
return -1;
}
}
public boolean isBalanced(TreeNode root) {
if(root == null) {
return true;
}
return height(root) >=0;
}
}
对称二叉树


附上代码
class Solution {
//创建一个方法,用来判断root的左树和 右数
public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree) {
if(leftTree == null && rightTree != null || leftTree != null && rightTree == null) {
return false;
}
if(leftTree == null &&rightTree == null) {
return true;
}
if(leftTree.val == rightTree.val) {
return isSymmetricChild(leftTree.left,rightTree.right) &&isSymmetricChild(leftTree.right,rightTree.left);
}
return false;
}
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return isSymmetricChild(root.left,root.right);
}
}
创建一颗二叉树
回忆上面我们创建一颗二叉树是不是 通过 穷举 来创建的一颗二叉树,那么 现在我们来使用先序遍历来创建一颗二叉树
二叉树遍历

附上代码
import java.util.Scanner;
import java.util.*;
class TreeNode{
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val) {
this.val = val;
}
}
public class Main{
public static int i= 0;
public static TreeNode createTree(String str) {
TreeNode root = null;
if(str.charAt(i) != '#') {
root = new TreeNode(str.charAt(i));
i++;
root.left = createTree(str);
root.right = createTree(str);
}else {
i++;
}
return root;
}
public static void inorter(TreeNode root){
if(root == null) {
return;
}
inorter(root.left);
System.out.print(root.val+" ");
inorter(root.right);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 这里需要注意hasNext 与 hasNextLine 的区别
// hasNextLine 遇到空格不结束
// hasNext 遇到空格结束
while(sc.hasNextLine()) {
String str = sc.nextLine();
TreeNode root = createTree(str);
inorter(root);
}
最后来补充一下之前的程序 遍历 ,回忆一下 上文 写 的 判断一颗完全二叉树
其实程序遍历也是 这个思路 通过 一个队列,为空 就 不 加入队列,
程序遍历
// 程序遍历
public void levelOrder(TreeNode root) {
if(root == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
TreeNode tmp = queue.poll();
System.out.print(tmp.val+ " ");
if(tmp.left!= null) {
queue.offer(tmp.left);
}
if(tmp.right != null) {
queue.offer(tmp.right);
}
}
}

写 完了 程序遍历那么我们上 力扣 上写一下oj题嘿嘿
二叉树的层序遍历
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
if(root == null) return list;
while(!queue.isEmpty()) {
int size = queue.size();
List<Integer> list2 = new ArrayList<>();
while(size != 0) {
TreeNode tmp = queue.poll();
list2.add(tmp.val);
if(tmp.left != null) {
queue.offer(tmp.left);
}
if(tmp.right != null) {
queue.offer(tmp.right);
}
size--;
}
list.add(list2);
}
return list;



边栏推荐
- ThreadLocal digs its knowledge points again
- Today's three interviews demo[integer ASCII class relationship]
- The largest rectangle in the bar graph of force buckle 84
- Persistence mechanism of redis
- 静态代码块Vs构造代码块
- Understanding mvcc in MySQL transactions is super simple
- 【mysql篇-基础篇】通用语法1
- fastadmin 安装后访问后台提示模块不存在
- VIM from dislike to dependence (20) -- global command
- Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介
猜你喜欢

That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade

DV scroll board width of datav rotation table component

IO管脚配置和pinctrl驱动

win10为任意文件添加右键菜单

多网络设备存在时,如何配置其上网优先级?

2022.06.26(LC_6100_统计放置房子的方式数)

即构「畅直播」,全链路升级的一站式直播服务

VIM from dislike to dependence (19) -- substitution

提高效率 Or 增加成本,开发人员应如何理解结对编程?

Lvgl description 3 about the use of lvgl Guide
随机推荐
Collection framework generic LinkedList TreeSet
一种太阳能电荷泵供电电路的方案设计
JS EventListener
SPARQL basic introductory exercise
Redis transactions
Fake constructor???
分析日志.log
Win10 add right-click menu for any file
Analysis log log
集合框架 泛型LinkedList TreeSet
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
The difference between ArrayList and LinkedList
Redis installation under Linux
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
RMAN-08137 主库无法删除归档文件
C# 解决使用SQLite 的相对路径问题
The largest rectangle in the bar graph of force buckle 84
静态代码块Vs构造代码块
内存泄露的最直接表现
2022.06.26 (LC Luo 6101 Luo determines whether the matrix is an X matrix)