public List<List> levelOrderBottom(TreeNode root) {
List<List> resList = new ArrayList<>();
if (root==null){
return resList;
}
Queue queue = new LinkedList();
queue.add(root);
while (!queue.isEmpty()){
int len = queue.size();
List list = new ArrayList<>();
while (len>0){
TreeNode poll = queue.poll();
list.add(poll.val);
if (poll.left!=null) queue.offer(poll.left);
if (poll.right!=null) queue.offer(poll.right);
len–;
}
resList.add(list);
}
Collections.reverse(resList);
return resList;
}
=======================================================================
注意右视图看到的元素指的不单单是右子树的元素,而是每一层最右侧的元素,这个元素也有可能出现在左子树,所以需要将每一层的最后一个元素加入集合
public List rightSideView(TreeNode root) {
List list = new ArrayList<>();
if (root==null){
return list;
}
Queue queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
int len = queue.size();
while (len>0){
TreeNode poll = queue.poll();
if (len==1){
list.add(poll.val);
}
if (poll.left!=null) queue.offer(poll.left);
if (poll.right!=null) queue.offer(poll.right);
len–;
}
}
return list;
}
========================================================================
利用层序遍历,记录每一层的数字并计算平均值即可
public List averageOfLevels(TreeNode root) {
List list = new ArrayList<>();
if (root==null){
return list;
}
Queue queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
double sum = 0;
int len = queue.size();
int temp=len;
while (len>0){
TreeNode poll = queue.poll();
sum+=poll.val;
len–;
if (poll.left!=null) queue.offer(poll.left);
if (poll.right!=null) queue.offer(poll.right);
}
list.add(sum/temp);
}
return list;
}
========================================================================
public List<List> levelOrder(Node root) {
List<List> resList = new ArrayList<>();
if (root==null){
return resList;
}
Deque queue = new LinkedList<>();
queue.offerLast(root);
while (!queue.isEmpty()){
int len = queue.size();
List list = new ArrayList<>();
for (int i=0;i<len;i++){
Node poll = queue.pollFirst();
list.add(poll.val);
List children = poll.children;
if (children==null||children.size()==0){
continue;
}
for (Node child : children) {
if (child!=null){
queue.offerLast(child);
}
}
}
resList.add(list);
}
return resList;
}
=========================================================================
public List largestValues(TreeNode root) {
List list = new ArrayList<>();
if (root==null){
return list;
}
Queue queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
int len = queue.size();
int max = Integer.MIN_VALUE;
for (int i=0;i<len;i++){
TreeNode poll = queue.poll();
if (poll.val>max){
max = poll.val;
}
if (poll.left!=null) queue.offer(poll.left);
if (poll.right!=null) queue.offer(poll.right);
}
list.add(max);
}
return list;
}
================================================================================
Queue queue = new LinkedList<>();
if (root==null){
return null;
}
queue.offer(root);
while (!queue.isEmpty()){
int len = queue.size();
Node nodePre = null;
Node node;
for (int i=0;i<len;i++){
if (i==0){
nodePre = queue.poll();
node = nodePre;
}else {
node = queue.poll();
nodePre.next = node;
nodePre = nodePre.next;
}
if (node.left!=null) queue.offer(node.left);
if (node.right!=null) queue.offer(node.right);
}
}
return root;
==================================================================================
这道题目说是二叉树,但第7题题目说是完整二叉树,其实没有任何差别,一样的代码一样的逻辑一样的味道
public Node connect2(Node root) {
Queue queue = new LinkedList<>();
if (root==null){
return null;
}
queue.offer(root);
while (!queue.isEmpty()){
int len = queue.size();
//处理第一个节点
Node top = queue.poll();
if (top.left!=null) queue.offer(top.left);
if (top.right!=null) queue.offer(top.right);
for (int i=1;i<len;i++){
Node poll = queue.poll();
if (poll.left!=null) queue.offer(poll.left);
if (poll.right!=null) queue.offer(poll.right);
top.next = poll;
top = top.next;
}
}
return root;
}
========================================================================
广度优先搜索解法:
public int maxDepth(TreeNode root) {
int max = 0;
Queue queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
int len = queue.size();
for (int i=0;i<len;i++){
TreeNode poll = queue.poll();
if (poll.left!=null) queue.offer(poll.left);
if (poll.right!=null) queue.offer(poll.right);
}
max++;
}
return max;
}
深度优先搜索解法:
public int maxDepth2(TreeNode root){
if (root==null){
return 0;
}
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
=========================================================================
注意,只有左右节点均为空时才有最小深度
if (root==null){
return 0;
}
Queue queue = new LinkedList();
queue.offer(root);
int min = 0;
while (!queue.isEmpty()){
int len = queue.size();
min++;
for (int i=0;i<len;i++){
TreeNode poll = queue.poll();
if (poll.leftnull&&poll.rightnull){
return min;
}else {
if (poll.left!=null) queue.offer(poll.left);
if (poll.right!=null) queue.offer(poll.right);
}
}
}
return min;
======================================================================
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。