leetcode-二叉树总结

版权声明:此文章为许诗宇所写,如需转载,请写下转载文章的地址 https://blog.csdn.net/xushiyu1996818/article/details/82757723

leetcode-104-二叉树的最大深度(maximum depth of binary tree)
查询树的深度,一般有两种方法
第一种,递归,长度=当前长度(1)+左右子树的max长度
第二种,一层层遍历,next为now的左右子树,然后now=next,直到now为空

leetcode-98-验证二叉搜索树(validat binary search tree)-java
二叉搜索树是左侧子树全部比root小,右侧全比root大
验证二叉搜索树,二种方法
1 中序遍历,左侧大于右侧
2 递归 令左侧 大于min 小于root 右侧 大于root 小于min

leetcode-101-对称二叉树(symmetric tree)-java
树的话,验证对称,可以用递归

leetcode- 102二叉树的层次遍历(binary tree level order traversal)-java
二叉树层次遍历
建立一个queue,一个rowlist,
一个现在层次的节点数num=1,一个下一层next=0
将root加入queue
当queue不为空
从queue取出头,num–,将值加入rowlist,将left,right加入queue尾部,next++
当num为0 则num=next,next=0,结果加入rowlist,rowlist新建一个

leetcode-108-将有序数组转换为二叉搜索树(convert sorted array to binary search tree)-java
二叉搜索树变为数组用中序遍历
数组变为平衡的二叉搜索树用递归
中间的为root
左边的比他小的数为左子树,右边的为右子树

猜你喜欢

转载自blog.csdn.net/xushiyu1996818/article/details/82757723