-2

I am learning DSA quite long.. During my learning, I encountered a concept 'Left View Method in Binary Tree'. I am completely scripted my code but there is a error when i tried to give a modified binary tree.

My binary tree view

    1
   / \
  2   3
 / \  / \
4   5 6  7
       \
        8

Actual left view is 1, 2, 4 but instead of I'm getting 1, 2, 4, 8, the 8 is right view also included. If your know to solve the problem, spot and comment the mistake. It will help in my learning Journey. Thanks in advance 🙂. ( code is below )

# left view method
class Node:
    def __init__(self, val):
        self.left = None
        self.right = None
        self.data = val

class Leftview:
    def view(self, root):
        out = []
        maxlevel = [-1]
        self._helperrecur(root, 0, maxlevel, out)
        for i in range(len(out)):
            print(out[i], end=' ')
        

    def _helperrecur(self, root, level, maxlevel, out):

        #base condition
        if root is None:
            return
        
        #main condition
        if level > maxlevel[0]:
            out.append(root.data)
            maxlevel[0] = level

        #recursive through tree
        self._helperrecur(root.left, level+1, maxlevel, out)
        self._helperrecur(root.right, level+1, maxlevel, out)


root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
root.right.left.right = Node(8)

lf = Leftview()
lf.view(root)

I am tried another way to change the recursion, left -> right previously but i changed to right -> left to check the right view. Right view is absolutely working fine.

Excepted outcome: 1, 3, 7, 8 My output: 1, 3, 7, 8

code is here,

self._helperrecur(root.right, level+1, maxlevel, out)
self._helperrecur(root.left, level+1, maxlevel, out)

I am expecting, How does right view is comes in left view ? If you knew comment me out,

3
  • 2
    Please define "Left View". Commented Oct 14, 2024 at 13:06
  • 1
    FWIW, the node labelled 8 is the leftmost node in its level - as well as the rightmost. You don't complain about node 1… Commented Oct 14, 2024 at 14:10
  • "Left view" is not a concept in what is commonly understood as "data structures and algorithms". This "left view" seems some creative concept invented on code challenge sites. No idea why "8" would not be seen by someone "at the left" of that drawing. Commented Oct 14, 2024 at 19:06

1 Answer 1

0

The left view is defined on GeeksForGeeks as "a set of leftmost nodes for every level."

The leftmost nodes at each level in your tree are 1, 2, 4, and 8. 8 is included in the set because as the only node at the last level, it is by definition the leftmost.

Sign up to request clarification or add additional context in comments.

8 Comments

Kind of interested to know why this answer would be downvoted.
They stated that "Actual left view is 1, 2, 4". The definition you found disagrees with that. The definition isn't up to us. It's their question. The right thing to do is to request the definition from them, wait for it, and then show that they misunderstood it (if that's the case, which I suspect it is). And it's especially bad if the definition you'd like to use comes from such a horribly bad site.
Thanks for your information Mischel, my doubt was solved !
@Sagar If this answered your question, please mark it as the accepted answer.
@nocomment Yes, the OP did incorrectly state that the actual left view is 1,2,4. My answer consists of pointing out the error and providing the correct information. An online search for "left view binary tree" returns many sites that give my definition. None that give the OP's definition.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.