看见这题,我十分惊叹,万千情绪只能用一个6形容 真是什么题目都能给你们想出来,还右视图,二叉树直接3d了我的天,二叉树自己都没想到
/** * 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) { auto ret =...
dfs(root.left, depth + 1, ans) 代码(Go) /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func rightSideView(root *TreeNode) []int { // ans 用于收集每一层最右侧结点的值 var ans []int // dfs 先...
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */classSolution{public:vector<int>rightSideView(TreeNode* root){if(root==nullptr)return{};queue<TreeNode*> q; q.push(root);intcurLevelNums =1;intnextLevelNums =0;vector<int> ans;while(!q.empty()){ TreeNode* nod...
英文网站:199. Binary Tree Right Side View 中文网站:199. 二叉树的右视图 问题描述 Given therootof a binary tree, imagine yourself standing on theright sideof it, returnthe values of the nodes you can see ordered from top to bottom.
TreeNode*temp =q.front(); q.pop(); layer.push_back(temp->val);if(temp->left != NULL) q.push(temp->left);if(temp->right != NULL) q.push(temp->right); } }returnres; } }; 3.Binary Tree Level Order Traversal II(https://leetcode.com/problems/binary-tree-level-order-traversal...
* 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) { ...
String[] args) {/*** 测试用例:* 1* 2 3* 5 4*/TreeNoderoot=newTreeNode(1);root.left=newTreeNode(2);root.right=newTreeNode(3);root.left.right=newTreeNode(5);root.right.right=newTreeNode(4);// 期望输出: 1,3,4for (Integerinteger : rightSideView(root)) {Syste...
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 to bottom. 题意 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
* Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{public List<Integer>rightSideView(TreeNode root){ArrayList<Integer>ret=newArrayList<Integer>();if(root==null)retu...