多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > [LeetCode] Binary Tree Right Side View

[LeetCode] Binary Tree Right Side View

來源:程序員人生   發布時間:2015-06-05 09:07:29 閱讀次數:2409次

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].

解題思路

層次遍歷法,找出每層最右真個結點。

實現代碼1

/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ // Runtime:7ms class Solution { public: vector<int> rightSideView(TreeNode *root) { vector<int> nums; queue<TreeNode*> nodes; if (root != NULL) { nodes.push(root); } TreeNode *cur; while (!nodes.empty()) { int size = nodes.size(); for (int i = 0; i < size; i++) { cur = nodes.front(); nodes.pop(); if (cur->left != NULL) { nodes.push(cur->left); } if (cur->right != NULL) { nodes.push(cur->right); } } nums.push_back(cur->val); } return nums; } };

實現代碼2

# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # Runtime:74ms class Solution: # @param root, a tree node # @return a list of integers def rightSideView(self, root): nums = [] if root == None: return nums nodes = [root] while nodes: size = len(nodes) for i in range(size): cur = nodes.pop(0) if cur.left != None: nodes.append(cur.left) if cur.right != None: nodes.append(cur.right) nums.append(cur.val) return nums;
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 看黄免费网站 | 久久久国产高清 | 亚洲最新视频在线观看 | 国产aⅴ自拍| 久久久久这里只有精品 | 性短视频在线观看免费不卡流畅 | 性欲影院| 中文字幕成人在线 | 国产成人精品曰本亚洲 | 中文字幕乱码二三区免费 | 欧美一级一毛片 | 天堂在线天堂最新版 | 午夜福利毛片 | 黄色的免费网站 | 欧美成人高清在线视频大全 | 日本高清精品动漫 | 免费观看欧美成人1314色 | 久久精品一区二区三区不卡 | 图片综合区 | 国产精品国产精品国产专区不卡 | 成人国产精品毛片 | 99999久爱视频在线观看 | 2018一级毛片免费观看 | 伊人网视频 | 国产好大好爽久久久久久久 | 国产免费亚洲 | 女人牲交一级毛片 | 一区二区三区毛片免费 | 最近最新高清中文字幕 | 久久丝袜精品综合网站 | 中文字幕第15页 | 在线亚洲日产一区二区 | 国产欧美日韩高清专区手机版 | 男女污污的视频 | 亚洲欧美另类日韩 | 欧美真人视频一级毛片 | 国产欧洲亚洲 | 国产伦精品一区二区三区在线观看 | 一级做a爰片久久毛片 | 亚洲看逼| 欧美人乱大交xxxxx |