问题链接 英文网站:199. Binary Tree Right Side View 中文网站:199. 二叉树的右视图 问题描述 Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top
TreeNode*node =q.front(); q.pop();if(node->left) q.push(node->left);if(node->right) q.push(node->right); } }returnres; } }; LeetCode All in One 题目讲解汇总(持续更新中...)
1、题目描述 2、问题分析 使用层序遍历 3、代码 1vector<int>v;2vector<int> rightSideView(TreeNode*root) {3if(root ==NULL)4returnv;56queue<TreeNode*>q;7q.push(root);89while(!q.empty()) {10intsize =q.size();11for(inti =0; i < size; i++) {12TreeNode *node =q.front();13...
我们同样可以借用层序遍历的思路,只要每次把这一层的最后一个元素取出来就行了,具体代码参见Binary Tree Traversal中的Binary Tree Level Order Traversal
LeetCode199题 Binary Tree Right Side View, 解题思路。 1、先读题,凡是同一层的,有右边的节点,只添加右边的。 2、用bfs去解决,队列先添加右子树,再添加左子树,队列除了带node 信息,还得有当前层数的信息。 3、循环处理,当前层数没有被使用,就添加node的val。
LeetCode Binary Tree Right Side View (DFS/BFS),题意:给一棵二叉树,要求收集每层的最后一个节点的值。按从顶到底装进vector返回。思路:BFS比较简单,先遍历右孩子就行了。1/**2*Definitionforabinarytreenode.3*structTreeNode{4*int...
Blogger:https://blog.baozitraining.org/2019/10/leetcode-solution-199-binary-tree-right.html Youtube:https://youtu.be/_g6pN64bF-o 博客园: https://www.cnblogs.com/baozitraining/p/11595617.html B站: https://www.bilibili.com/video/av69112013/ ...
好久没有刷leetcode了,要继续了。。 今天第一题。 Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, ...
http://www.programcreek.com/2014/04/leetcode-binary-tree-right-side-view-java/ 看了这篇博客就懂了。 ** 总结: queue来遍历tree ** Anyway, Good luck, Richardo! My code: /** * Definition for a binary tree node. * public class TreeNode { ...
01.Binary Tree Inorder Traversal.wmv 02.Path Sum.wmv 03.Maximum Depth of Binary Tree.wmv 55.LeetCode 刷题 – 常用算法思想 笔记和源码 55. LeetCode 刷题 – 常用算法思想.mht 01.贪心算法思想 .wmv 02.回溯算法思想.wmv 03.动态规划算法思想.wmv 56.OpenCV – 安装和简介 笔记和源码 opencv需要的...