Lakhankumawat / LearnCPP

Learn Cpp from Beginner to Advanced ✅ Practice 🎯 Code 💻 Repeat 🔁 One step solution for c++ beginners and cp enthusiasts.
https://lakhankumawat.github.io/LearnCPP/
MIT License
598 stars 484 forks source link

right view of bt #1538

Closed Dharmesh177 closed 2 years ago

Dharmesh177 commented 2 years ago

Related Issue

Proposed Changes

Additional Info

Checklist

-Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

https://github.com/Lakhankumawat/LearnCPP/issues/1524

The problem can be solved using simple recursive traversal. We can keep track of the level of a node by passing a parameter to all recursive calls. The idea is to keep track of the maximum level also. And traverse the tree in a manner that the right subtree is visited before the left subtree. Whenever we see a node whose level is more than the 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 the left subtree)

-visual studio code platform is used

Output Screenshots

Screenshot #1 Screenshot #2
Output Code
image I

image |

Dharmesh177 commented 2 years ago

review my PR @Lakhankumawat

github-actions[bot] commented 2 years ago

This PR is stale because it has been open 3 days with no activity. Please commit the changes requested or make improvements on the code. Thank you for your contributions!