Side view of binary tree Algorithm(Left and right side view)

preview_player
Показать описание
Side view of binary tree. Left side view and right side view.

Рекомендации по теме
Комментарии
Автор

People complaining about the code, it is pretty simple. It's just a minor change from level order traversal.
In level order traversal, you print every node per iteration of the queue.
For right side view, per iteration, you will print the last node only. It will automatically be the rightmost.
For left side view, per iteration, you will print the first node only. It will automatically be the leftmost.
Code : (for right most)
vector<int>ans;
TreeNode*top=NULL;
void rightView(TreeNode*root){
queue<TreeNode*>q;
q.push(root);
while(!q.empty()){
int sz=q.size();
for(int i=0;i<sz;i++){
top = q.front();q.pop();


}
ans.push_back(top->val);
}
}

AbhishekKumar-yvih
Автор

Wow! 50 shades of printing of a binary tree. Thanks man :)

reyou
Автор

you have to explain code also or approach

NeeruSinghK
Автор

Great! So we can use level order traversal to get all views, top, bottom, left, right, of the tree!!!

sourabhpatil
Автор

Love you sir....May god bless you and keep you safe....I am very much thankful to you sir....

dipakkumarsinha
Автор

Plz make videos on how to calculate Time complexity of program.
Thanks in advance

manasideshmukh
Автор

Please post a video on time complexity of heaps data structures

rubinshah
Автор

You explain very nicely..!!
Thanks for explanation..!!

Sushil
Автор

U explained the problem but where is the solution??
By the way i like your videos
Np for this one🙂

krishnaverma
Автор

All Views: Left, Right, Top, Bottom. Hope you find that useful:-)

l1=left
l2=right
l3=top
l4=bottom

class Node:
def __init__(self, data):
self.data=data
self.left=None
self.right=None
def level(root):
currLevel=[]
nextLevel=[]
l1, l2, l3, l4=[], [], [], []
x, y, d={}, {}, {}
currLevel.append(root)
h=0
l1.append(root.data)
l2.append(root.data)
d[root.data]=h
while(len(currLevel)>0):
temp=currLevel.pop(0)
if(temp.left):
nextLevel.append(temp.left)
h=d[temp.data]-1
d[temp.left.data]=h
if(temp.right):
nextLevel.append(temp.right)
h=d[temp.data]+1
d[temp.right.data]=h
if(len(currLevel)==0):
if(nextLevel!=[]):
l1.append(nextLevel[0].data)

currLevel, nextLevel=nextLevel, currLevel
for i in d:
if d[i] not in x:
x[d[i]]=i
for i in d:
y[d[i]]=i
for i in sorted(x):
l3.append(x[i])
for i in sorted(x):
l4.append(y[i])
return l1, l2, l3, l4
root=Node(0)
root.left=Node(1)
root.right=Node(2)
root.left.left=Node(3)
root.left.right=Node(5)
root.right.left=Node(6)
root.right.right=Node(11)
root.left.left.left=Node(4)
root.right.left.left=Node(7)



print(level(root))

ananyakashyap
Автор

great visualization.
essentially, collect all the depth as keys and each depth's values, recursively which orders from left to right. Then all we have to do is to iterate through each depth and collect its last value and append it to a list, giving us a view of the rightmost value from the right pov. Like I said once, you see the tree in a stack like structure with arrays of values at each depth, the code implementation becomes easier. Just in case, here's my python solution:

class Solution:
def rightSideView(self, root):
depthStack = {}
ans = []

self.depthStackFiller(root, depthStack, 0)

for depth in depthStack:

print('depthStack: ', depthStack)
return ans

def depthStackFiller(self, root, stack, depth):
if not root:
return

if depth not in stack:
stack[depth] = [root.val]
else:
stack[depth].append(root.val)
self.depthStackFiller(root.left, stack, depth+1)
self.depthStackFiller(root.right, stack, depth+1)

kingjohnkk
Автор

pre order traversal can easily solve it

MrDeepak
visit shbcf.ru