[GeeksForGeeks] Print leftmost and rightmost nodes at each level of a binary tree.
2021-06-14 18:03
标签:and each sem log get public width add always Given a Binary Tree, Print the corner nodes at each level. The node at the leftmost and the node at the rightmost. For example, output for following is 15, 10, 20, 8, 25. Solution. Level Order Traversal using queue. Core idea: Level order traversal always visit nodes of one level from left to right. And we know the number of nodes at each level by pre-reading the size of the queue. [GeeksForGeeks] Print leftmost and rightmost nodes at each level of a binary tree. 标签:and each sem log get public width add always 原文地址:http://www.cnblogs.com/lz87/p/7277712.html 1 import java.util.ArrayList;
2 import java.util.LinkedList;
3 import java.util.Queue;
4
5 class TreeNode {
6 TreeNode left;
7 TreeNode right;
8 int val;
9 TreeNode(int val){
10 this.left = null;
11 this.right = null;
12 this.val = val;
13 }
14 }
15 public class Solution {
16 public ArrayList
文章标题:[GeeksForGeeks] Print leftmost and rightmost nodes at each level of a binary tree.
文章链接:http://soscw.com/essay/93966.html