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

首頁 > 學院 > 開發設計 > 正文

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

2019-11-09 20:44:53
字體:
來源:轉載
供稿:網友

題目內容

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

題目簡述

從上到下寫出二叉樹每層最右側節點對應的數值

題目分析

按層次遞歸,并將層數作為遞歸變量使每層只記錄一個數值,從右至左遍歷則使每層右邊節點最先遍歷,該節點數值即可在本層唯一記錄。

代碼示例

/** * 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; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 牙克石市| 潮州市| 万山特区| 广宁县| 尖扎县| 增城市| 灵璧县| 台前县| 榆中县| 阜新市| 闵行区| 九龙县| 南江县| 类乌齐县| 繁峙县| 长汀县| 广德县| 遵化市| 邹城市| 嵩明县| 卢龙县| 德惠市| 上思县| 通许县| 清涧县| 盖州市| 侯马市| 洪洞县| 凤城市| 德钦县| 台中市| 四子王旗| 木里| 江永县| 修水县| 通山县| 扎囊县| 浑源县| 南通市| 洱源县| 静乐县|