标签:void tor sid ide nbsp back public 商业 http
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-right-side-view
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/** * 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 { int deep[105]; vector<int> vec; public: vector<int> rightSideView(TreeNode* root) { init(); dfs(root, 1); return vec; } void dfs(TreeNode* root, int _deep) { if (root== nullptr) return; if(! deep[_deep]) { vec.push_back(root->val); deep[_deep] = 1; } _deep++; dfs(root->right, _deep); dfs(root->left, _deep); } void init() { vec.clear(); for (int i = 0; i < 101; i ++) { deep[i] = 0; } } };
标签:void tor sid ide nbsp back public 商业 http
原文地址:https://www.cnblogs.com/Amaris-diana/p/13272117.html