199 Binary Tree Right Side View Given 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.
For example: Given the following binary tree,
1 <— / / 2 3 <— / / 5 4 <—
You should return [1, 3, 4].
Credits: Special thanks to @amrsaqr for adding this PRoblem and creating all test cases. 題目原文
從上到下寫出二叉樹每層最右側(cè)節(jié)點(diǎn)對應(yīng)的數(shù)值
按層次遞歸,并將層數(shù)作為遞歸變量使每層只記錄一個數(shù)值,從右至左遍歷則使每層右邊節(jié)點(diǎn)最先遍歷,該節(jié)點(diǎn)數(shù)值即可在本層唯一記錄。
|
新聞熱點(diǎn)
疑難解答