haoel / leetcode

LeetCode Problems' Solutions
17.58k stars 4.91k forks source link

[Binary Tree Right Side View] Reduce the lines of code to around one third #65

Closed zhuangh closed 9 years ago

zhuangh commented 9 years ago

Hi @haoel

Thanks for sharing the code @ https://github.com/haoel/leetcode/blob/master/algorithms/binaryTreeRightSideView/binaryTreeRightSideView.cpp

Just FYI, the solution of mine is a little bit shorter (https://github.com/zhuangh/OJ/blob/master/leetcode/cpp/binaryTreeRightSideView.cpp) than yours. The number of lines is 1/3 of the solution you shared.

The idea is to use DFS to do pre-order traversal of the mirror tree. At the same time, use an array to mark current level, so that we can keep from putting wrong element into the result vector.

Thank you

haoel commented 9 years ago

Hi @zhuangh

I think you probably didn't read my code carefully. Actually, I wrote two version of the solution, one is recursive version, another one is non-recursive version. (Although they have same function name but the function signature is different, and in the real function, I use random() to invoke these two helper )

Anyway, Thanks so much left your solution here.

P.S. the recursive version's helper just only 4 lines of code, it's almost 1/2 shorter than yours. ;-)

zhuangh commented 9 years ago

My bad. Great! Thanks for pointing out!