仅仅遍历并输出全部元素。
List<Integer> simpleLevelOrder(TreeNode root) {
if (root == null) {
return new ArrayList<Integer>();
}
List<Integer> res = new ArrayList<Integer>();
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (queue.size() > 0) {
TreeNode t = queue.remove();
res.add(t.val);
if (t.left != null) {
queue.add(t.left);
}
if (t.right != null) {
queue.add(t.right);
}
}
return res;
}
用 size 标记。
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) {
return new ArrayList<List<Integer>>();
}
List<List<Integer>> res = new ArrayList<List<Integer>>();
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (queue.size() > 0) {
// 获取当前层的元素个数
int size = queue.size();
ArrayList<Integer> temp = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
TreeNode t = queue.remove();
temp.add(t.val);
if (t.left != null) {
queue.add(t.left);
}
if (t.right != null) {
queue.add(t.right);
}
}
res.add(temp);
}
return res;
}
自底向上,逐层从左向右遍历。
和从根节点开始遍历基本一样,只不过是在往结果里存储的时候顺序改变。
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> levelOrder = new LinkedList<List<Integer>>();
if (root == null) {
return levelOrder;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> level = new ArrayList<Integer>();
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
level.add(node.val);
TreeNode left = node.left, right = node.right;
if (left != null) {
queue.offer(left);
}
if (right != null) {
queue.offer(right);
}
}
// add(index, element)
// 将元素添加到指定的索引处,原来的链表的索引向后移动
// 每次都往索引0处放,达到后来的先放的效果
levelOrder.add(0, level);
}
return levelOrder;
}
锯齿形层序遍历就是先从左往右遍历一层,再从右往左遍历一层。
和上面的算法基本一样,只不过要用一个布尔型的变量指示从左向右还是从右向左,从右向左的话就需要从链表的前边进行添加。
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> ans = new LinkedList<List<Integer>>();
if (root == null) {
return ans;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
// 为true是从左向右遍历,为false是从右向左遍历
boolean isOrderLeft = true;
while (!queue.isEmpty()) {
Deque<Integer> levelList = new LinkedList<Integer>();
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode curNode = queue.poll();
if (isOrderLeft) {
levelList.offerLast(curNode.val);
} else {
levelList.offFirst(curNode.val);
}
if (curNode.left != null) {
queue.offer(curNode.left);
}
if (curNode.right != null) {
queue.offer(curNode.right);
}
}
ans.add(levelList);
isOrderLeft = !isOrderLeft;
}
return ans;
}
public List<List<Integer>> nLevelOrder(Node root) {
List<List<Integer>> value = new ArrayList<>();
Deque<Node> q = new ArrayDeque<>();
if (root != null) {
q.addLast(root);
}
while (!q.isEmpty()) {
Deque<Node> next = new ArrayDeque<>();
List<Integer> nd = new ArrayList<>();
while (!q.isEmpty()) {
Node cur = q.pollFirst();
nd.add(cur.val);
for (Node chd : cur.children) {
if (chd != null) {
next.add(chd);
}
}
}
q = next;
value.add(nd);
}
return value;
}
用一个变量记录每行的最大值就好了。
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> deque = new ArrayDeque<>();
if (root != null) {
deque.addLast(root);
}
while (!deque.isEmpty()) {
int size = deque.size();
int levelMaxNum = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode node = deque.poll();
levelMaxNum = Math.max(node.val, levelMaxNum);
if (node.left != null) {
deque.addLast(node.left);
}
if (node.right != null) {
deque.addLast(node.right);
}
}
res.add(levelMaxNum);
}
return res;
}
public List<Double> averageOfLevels(TreeNode root) {
List<Double> res = new ArrayList<>();
if (root == null) {
return res;
}
Queue<TreeNode> list = new LinkedList<>();
list.add(root);
while (list.size() != 0) {
int len = list.size();
double sum = 0;
for (int i = 0; i < len; i++) {
TreeNode node = list.poll();
sum += node.val;
if (node.left != null) {
list.add(node.left);
}
if (node.right != null) {
list.add(node.right);
}
}
res.add(sum/len);
}
return res;
}
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
if (i == size - 1) {
res.add(node.val);
}
}
}
return res;
}
回想锯齿形遍历时的isOrderLeft,这道题就是这个值一直取false,即从右向左遍历。那么就可以先添加右节点,再添加左节点,达到反过来的效果。
public int findBottomLeftValue(TreeNode root) {
if (root.left == null && root.right == null) {
return root.val;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
TreeNode temp = new TreeNode(0);
while (!queue.isEmpty()) {
temp = queue.poll();
if (temp.right != null) {
queue.offer(temp.right);
}
if (temp.left != null) {
queue.offer(temp.left);
}
}
return temp.val;
}
【持续更新】。
如果对您有帮助,请点赞关注支持我,谢谢!❤
如有错误或者不足之处,敬请指正!❤
个人主页:星不易 ♥
算法通关村专栏:不易|算法通关村 ♥