[GeeksForGeeks] Print leftmost and rightmost nodes at each level of a binary tree.

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.

 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<TreeNode> getLeftRightMostAtEachLevel(TreeNode root) {
17         ArrayList<TreeNode> result = new ArrayList<TreeNode>();
18         if(root == null){
19             return result;
20         }
21         Queue<TreeNode> queue = new LinkedList<TreeNode>();
22         queue.offer(root);
23         while(!queue.isEmpty()){
24             int size = queue.size();
25             for(int i = 0; i < size; i++){
26                 TreeNode curr = queue.poll();
27                 if(i == 0){
28                     result.add(curr);
29                 }
30                 if(i > 0 && i == size - 1){
31                     result.add(curr);
32                 }
33                 if(curr.left != null){
34                     queue.offer(curr.left);
35                 }
36                 if(curr.right != null){
37                     queue.offer(curr.right);
38                 }
39             }
40         }
41         return result;
42     }
43 }
时间: 2024-08-17 19:42:56

[GeeksForGeeks] Print leftmost and rightmost nodes at each level of a binary tree.的相关文章

[GeeksForGeeks] Print all nodes that don&#39;t have sibling in a binary tree.

Given a binary tree,  get all nodes that don't have sibling node, excluding the root node. Level order and pre order solutions. 1 import java.util.ArrayList; 2 import java.util.LinkedList; 3 import java.util.Queue; 4 5 public class NodeWithoutSibling

Print Nodes in Top View of Binary Tree

Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes can be printed in any order. Expected time complexity is O(n) A node x is there in output if x

U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column

Give a binary tree, elegantly print it so that no two tree nodes share the same column. Requirement: left child should appear on the left column of root, and right child should appear on the right of root. Example: a b c d e f z g h i j 这道题若能发现inorde

[GeeksForGeeks] Connect binary tree nodes of same level

Given a binary tree with each node having one extra field of nextRight. nextRight points to the next right node of the same level. Initially all nodes' nextRight field are set to null. Connect all nodes at the same level by setting each node's nextRi

[GeeksForGeeks] Remove all half nodes of a given binary tree

Given A binary Tree, how do you remove all the half nodes (which has only one child)? Note leaves should not be touched as they have both children as NULL. For example consider the below tree. Nodes 7, 5 and 9 are half nodes as one of their child is

[geeksforgeeks] Convert a given Binary Tree to Doubly Linked List

http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Binary Tree (Bt), convert it to a Doubly Linked List(DLL). The left and right pointers in nodes are to be used as previous and next pointers respectively

[geeksforgeeks] Bottom View of a Binary Tree

http://www.geeksforgeeks.org/bottom-view-binary-tree/ Bottom View of a Binary Tree Given a Binary Tree, we need to print the bottom view from left to right. A node x is there in output if x is the bottommost node at its horizontal distance. Horizonta

[GeeksForGeeks] Level order traversal in spiral form of a binary tree.

Write a function to print spiral order traversal of a binary tree. For below tree, function should print 1, 2, 3, 4, 5, 6, 7.    Solution. For a normal level order traversal of a binary tree, we traverse every level from left to right. To achieve thi

655. Print Binary Tree 解题报告(树)

第一部分:搜索.遍历 [例子1]655. Print Binary Tree Example 1: Input: 1 / 2 Output: [["", "1", ""], ["2", "", ""]] Example 2: Input: 1 / 2 3 4 Output: [["", "", "", "1"