二叉树的右视图—leetcode199
生活随笔
收集整理的這篇文章主要介紹了
二叉树的右视图—leetcode199
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一棵二叉樹,想象自己站在它的右側,按照從頂部到底部的順序,返回從右側所能看到的節點值。
示例:
輸入:?[1,2,3,null,5,null,4] 輸出:?[1, 3, 4] 解釋: 1 <---/ \ 2 3 <---\ \5 4 <---?
/*** 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:vector<int> rightSideView(TreeNode* root) {vector<int> res;queue<TreeNode*> vec;if(root==NULL)return res;vec.push(root);while(!vec.empty()){vector<int> temp;int n = vec.size();for(int i=0;i<n;++i){TreeNode* topnode = vec.front();vec.pop();if(topnode->left!=NULL)vec.push(topnode->left);if(topnode->right!=NULL)vec.push(topnode->right);temp.push_back(topnode->val);}res.push_back(temp.back());}return res;} };?
總結
以上是生活随笔為你收集整理的二叉树的右视图—leetcode199的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二叉树的锯齿形层次遍历—leetcode
- 下一篇: 字符串相加—leetcode415