国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

[LeetCode 199] Binary Tree Right Side View (遞歸的層數(shù))

2019-11-10 16:57:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題目?jī)?nèi)容

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. 題目原文

題目簡(jiǎn)述

從上到下寫(xiě)出二叉樹(shù)每層最右側(cè)節(jié)點(diǎn)對(duì)應(yīng)的數(shù)值

題目分析

按層次遞歸,并將層數(shù)作為遞歸變量使每層只記錄一個(gè)數(shù)值,從右至左遍歷則使每層右邊節(jié)點(diǎn)最先遍歷,該節(jié)點(diǎn)數(shù)值即可在本層唯一記錄。

代碼示例

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: void recursion(vector<int> &res,TreeNode *root,int level) { if(root==NULL) return; if(res.size()<level) res.push_back(root->val); recursion(res,root->right,level+1); recursion(res,root->left,level+1); } vector<int> rightSideView(TreeNode *root) { vector<int> res; recursion(res,root,1); return res; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平舆县| 泸水县| 扶风县| 濮阳县| 安泽县| 广河县| 哈尔滨市| 印江| 梁平县| 鱼台县| 柳林县| 蒙阴县| 论坛| 仁寿县| 凤山县| 固原市| 芒康县| 井研县| 吉安市| 河北省| 浠水县| 银川市| 长垣县| 江华| 兴化市| 尚志市| 泗阳县| 黄大仙区| 丹巴县| 黄浦区| 太谷县| 上饶县| 怀集县| 祁东县| 丰县| 红河县| 广德县| 安远县| 康乐县| 开阳县| 墨脱县|