Print Left View of a Binary Tree | GeeksforGeeks
Given a Binary Tree, print left view of it. Left view of a Binary Tree is set of nodes visible when tree is visited from left side.
The problem can also be solved using simple recursive traversal. We can keep track of level of a node by passing a parameter to all recursive calls. The idea is to keep track of maximum level also. Whenever we see a node whose level is more than maximum level so far, we print the node because this is the first node in its level (Note that we traverse the left subtree before right subtree).
Print Right View of a Binary Tree
Given a Binary Tree, print Right view of it. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side.
We can keep track of level of a node by passing a parameter to all recursive calls. The idea is to keep track of maximum level also. And traverse the tree in a manner that right subtree is visited before left subtree. Whenever we see a node whose level is more than maximum level so far, we print the node because this is the last node in its level (Note that we traverse the right subtree before left subtree).
http://www.programcreek.com/2014/04/leetcode-binary-tree-right-side-view-java/
http://buttercola.blogspot.com/2015/04/leetcode-binary-tree-right-side-view.html
Use Queue interface, poll and offer methods.
http://blog.welkinlan.com/2015/04/07/binary-tree-right-side-view-leetcode-java/
Left then right. But don't need to maintain count of this level and next level because when we start to iterate one layer, only elements in that level are in the queue.
http://siyang2leetcode.blogspot.com/2015/04/binary-tree-right-side-view.html
Use two queues, switch them after iterate this level.
Read full article from Print Left View of a Binary Tree | GeeksforGeeks
Given a Binary Tree, print left view of it. Left view of a Binary Tree is set of nodes visible when tree is visited from left side.
The problem can also be solved using simple recursive traversal. We can keep track of level of a node by passing a parameter to all recursive calls. The idea is to keep track of maximum level also. Whenever we see a node whose level is more than maximum level so far, we print the node because this is the first node in its level (Note that we traverse the left subtree before right subtree).
void
leftViewUtil(
struct
node *root,
int
level,
int
*max_level)
{
// Base Case
if
(root==NULL)
return
;
// If this is the first node of its level
if
(*max_level < level)
{
printf
(
"%d\t"
, root->data);
*max_level = level;
}
// Recur for left and right subtrees
leftViewUtil(root->left, level+1, max_level);
leftViewUtil(root->right, level+1, max_level);
}
// A wrapper over leftViewUtil()
void
leftView(
struct
node *root)
{
int
max_level = 0;
leftViewUtil(root, 1, &max_level);
}
Also check
http://www.geeksforgeeks.org/print-right-view-binary-tree-2/
http://massivealgorithms.blogspot.com/2014/07/algorithms-and-me-binary-search-tree_19.html
Print Right View of a Binary Tree
Given a Binary Tree, print Right view of it. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side.
We can keep track of level of a node by passing a parameter to all recursive calls. The idea is to keep track of maximum level also. And traverse the tree in a manner that right subtree is visited before left subtree. Whenever we see a node whose level is more than maximum level so far, we print the node because this is the last node in its level (Note that we traverse the right subtree before left subtree).
void
rightViewUtil(
struct
Node *root,
int
level,
int
*max_level)
{
// Base Case
if
(root==NULL)
return
;
// If this is the last Node of its level
if
(*max_level < level)
{
printf
(
"%d\t"
, root->data);
*max_level = level;
}
// Recur for right subtree first, then left subtree
rightViewUtil(root->right, level+1, max_level);
rightViewUtil(root->left, level+1, max_level);
}
http://www.programcreek.com/2014/04/leetcode-binary-tree-right-side-view-java/
http://buttercola.blogspot.com/2015/04/leetcode-binary-tree-right-side-view.html
Use Queue interface, poll and offer methods.
public List<Integer> rightSideView(TreeNode root) { ArrayList<Integer> result = new ArrayList<Integer>(); if(root == null) return result; LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); while(queue.size() > 0){ //get size here int size = queue.size(); for(int i=0; i<size; i++){ TreeNode top = queue.remove(); //the first element in the queue (right-most of the tree) if(i==0){ result.add(top.val); } //add right first if(top.right != null){ queue.add(top.right); } //add left if(top.left != null){ queue.add(top.left); } } } return result; } |
Left then right. But don't need to maintain count of this level and next level because when we start to iterate one layer, only elements in that level are in the queue.
http://siyang2leetcode.blogspot.com/2015/04/binary-tree-right-side-view.html
Use two queues, switch them after iterate this level.
public List<Integer> rightSideView(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
List<TreeNode> cur_layer = new ArrayList<TreeNode>();
// edge case
if(root==null) return list;
// initialize current layer
cur_layer.add(root);
// level-order traversal
while(!cur_layer.isEmpty()){
list.add(cur_layer.get(cur_layer.size()-1).val);
List<TreeNode> next_layer = new ArrayList<TreeNode>();
for(TreeNode node: cur_layer){
if(node.left!=null) next_layer.add(node.left);
if(node.right!=null) next_layer.add(node.right);
}
cur_layer = next_layer;
}
return list;
}