目录
层序遍历
102 二叉树的层序遍历
递归
迭代
107 二叉树的层序遍历 ||
递归
迭代
199 二叉树的右视图
637 二叉树的层平均值
429 N叉树的层序遍历
515 在每个树行中寻找最大值
116 填充每个节点的下一个右侧节点指针
117 填充每个节点的下一个右侧节点||
104 二叉树的最大深度
111 二叉树的最小深度
226 翻转二叉树
递归
迭代
101 对称二叉树
递归
迭代
class Solution {
List> resList = new ArrayList>();
public List> levelOrder(TreeNode root) {
int depth = 0;
check(root,depth);
return resList;
}
private void check(TreeNode root,Integer depth){
if(root == null)return;
depth++;
if(resList.size() < depth){
List newList = new ArrayList<>();
resList.add(newList);
}
resList.get(depth - 1).add(root.val);
check(root.left,depth);
check(root.right,depth);
}
}
时间复杂度O(n)
空间复杂度O(h)h为二叉树的高度
class Solution {
public List> levelOrder(TreeNode root) {
List>res = new ArrayList>();
if(root == null)return res;
Dequest = new LinkedList<>();
st.add(root);
while(!st.isEmpty()){
ListnewList = new ArrayList<>();
int siz = st.size();
for(int i = 0;i < siz;i++){
TreeNode cur = st.pollFirst();
newList.add(cur.val);
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
}
res.add(newList);
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n)为栈所需空间
class Solution {
List>resList = new ArrayList>();
public List> levelOrderBottom(TreeNode root) {
int depth = 0;
check(root,depth);
List>res = new ArrayList>();
for(int i = resList.size() - 1;i >= 0;i--){
res.add(resList.get(i));
}
return res;
}
private void check(TreeNode root,Integer depth){
if(root == null)return;
depth++;
if(resList.size() < depth){
ListnewList = new ArrayList<>();
resList.add(newList);
}
resList.get(depth - 1).add(root.val);
check(root.left,depth);
check(root.right,depth);
}
}
时间复杂度O(n)
空间复杂度O(n) h为二叉树的高度,n为resList的大小,任何情况下都满足 h≤n,所以为O(n)
class Solution {
public List> levelOrderBottom(TreeNode root) {
List>res = new ArrayList>();
if(root == null)return res;
Dequest = new LinkedList();
st.add(root);
while(!st.isEmpty()){
int siz = st.size();
ListnewList = new ArrayList<>();
for(int i = 0;i < siz;i++){
TreeNode cur = st.pollFirst();
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
newList.add(cur.val);
}
res.add(newList);
}
List>ares = new ArrayList>();
for(int i = res.size() - 1;i >= 0;i--){
ares.add(res.get(i));
}
return ares;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public List rightSideView(TreeNode root) {
Listres = new ArrayList<>();
Dequest = new LinkedList<>();
if(root == null)return res;
st.addLast(root);
while(!st.isEmpty()){
int siz = st.size();
for(int i = 0;i < siz;i++){
TreeNode cur = st.pollFirst();
if(cur.left != null)st.addLast(cur.left);
if(cur.right != null)st.addLast(cur.right);
if(i == siz - 1)res.add(cur.val);
}
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n)每个节点最多入栈一次
class Solution {
public List averageOfLevels(TreeNode root) {
Listres = new ArrayList<>();
Deque st = new LinkedList<>();
if(root == null)return res;
st.add(root);
while(!st.isEmpty()){
Double sum = 0.0;
int siz = st.size();
for(int i = 0;i < siz;i++){
TreeNode cur = st.poll();
sum += cur.val;
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
}
res.add(sum / siz);
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public List> levelOrder(Node root) {
List>res = new ArrayList>();
Dequest = new LinkedList<>();
if(root == null)return res;
st.add(root);
while(!st.isEmpty()){
ListnewList = new ArrayList<>();
int siz = st.size();
for(int i = 0;i < siz;i++){
Node cur = st.poll();
newList.add(cur.val);
Listchildrens = cur.children;
if(childrens == null || childrens.size() == 0)continue;
for(Node node : childrens){
if(node != null)st.add(node);
}
}
res.add(newList);
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n) 即队列需要的空间大小,最坏情况下树为两层,第二层有n - 1个节点
class Solution {
public List largestValues(TreeNode root) {
Listres = new ArrayList();
Dequest = new LinkedList<>();
if(root == null)return res;
st.add(root);
while(!st.isEmpty()){
int siz = st.size();
int mn = Integer.MIN_VALUE;
for(int i = 0;i < siz;i++){
TreeNode cur = st.pollFirst();
mn = Math.max(mn,cur.val);
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
}
res.add(mn);
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public Node connect(Node root) {
Dequest = new LinkedList<>();
if(root == null)return null;
st.add(root);
while(!st.isEmpty()){
int siz = st.size();
Node cur = st.pollFirst();
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
for(int i = 1;i < siz;i++){//cur已经取出,下标从1开始
Node next = st.pollFirst();
if(next.left != null)st.add(next.left);
if(next.right != null) st.add(next.right);
cur.next = next;
cur = next;
}
}
return root;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public Node connect(Node root) {
Dequest = new LinkedList<>();
if(root == null)return null;
st.add(root);
while(!st.isEmpty()){
int siz = st.size();
Node cur = st.pollFirst();
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
for(int i = 1;i < siz;i++){
Node next = st.pollFirst();
if(next.left != null)st.add(next.left);
if(next.right != null)st.add(next.right);
cur.next = next;
cur = next;
}
}
return root;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public int maxDepth(TreeNode root) {
int depth = 0;
Dequest = new LinkedList<>();
if(root == null)return depth;
st.add(root);
while(!st.isEmpty()){
int siz = st.size();
for(int i = 0;i < siz;i++){
TreeNode cur = st.pollFirst();
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
}
depth++;
}
return depth;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public int minDepth(TreeNode root) {
int depth = 0;
Dequest = new LinkedList<>();
if(root == null)return 0;
st.add(root);
while(!st.isEmpty()){
int siz = st.size();
depth++;
for(int i = 0;i < siz;i++){
TreeNode cur = st.pollFirst();
if(cur.left == null && cur.right == null)return depth;
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
}
}
return -1;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null)return null;
invertTree(root.left);
invertTree(root.right);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
时间复杂度O(n)
空间复杂度O(n) 使用的空间由递归栈的深度决定,最坏情况下二叉树为链状,此时为O(n)
class Solution {
public TreeNode invertTree(TreeNode root) {
Dequest = new LinkedList<>();
if(root == null)return null;
st.add(root);
while(!st.isEmpty()){
int siz = st.size();
for(int i = 0;i < siz;i++){
TreeNode cur = st.pollFirst();
TreeNode temp = cur.right;
cur.right = cur.left;
cur.left = temp;
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
}
}
return root;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public boolean isSymmetric(TreeNode root) {
return compare(root.left,root.right);
}
private boolean compare(TreeNode l,TreeNode r){
if(l == null && r == null)return true;
else if(l == null && r != null)return false;
else if(l != null && r == null)return false;
if(l.val != r.val)return false;
boolean c1 = compare(l.left,r.right);
boolean c2 = compare(l.right,r.left);
return c1 && c2;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public boolean isSymmetric(TreeNode root) {
Dequedeque = new LinkedList<>();
deque.offerFirst(root.left);
deque.offerLast(root.right);
while(!deque.isEmpty()){
TreeNode l = deque.pollFirst();
TreeNode r = deque.pollLast();
if(l == null && r == null)continue;
if(l == null && r != null)return false;
if(l != null && r == null)return false;
if(l.val != r.val)return false;
deque.offerFirst(l.left);
deque.offerFirst(l.right);
deque.offerLast(r.right);
deque.offerLast(r.left);
}
return true;
}
}
时间复杂度O(n)
空间复杂度O(n)