Binary Tree Right Side View--LeetCode

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

思路:相当于层次遍历,只不过需要注意的是指输出这一层的最后一个元素。

void rightSideView(BinTree *root)
{
     vector<int> vec;
     deque<BinTree*> de;
     if(root == NULL)
      return ;
     int curnum = 1;
     int nexnum=0;
     de.push_back(root);
     BinTree* temp;
     while(!de.empty())
     {
         while(curnum >0)
         {
             temp = de.front();
             if(curnum ==1)
               vec.push_back(temp->value);
             de.pop_front();
             curnum--;
             if(temp->left != NULL)
             {
                de.push_back(temp->left);
                nexnum++;
             }

             if(temp->right != NULL)
             {
                 de.push_back(temp->right);
                 nexnum++;
             }

         }
         curnum = nexnum;
         nexnum=0;
     }
     for(int i=0;i<vec.size();i++)
      cout<<vec[i]<<" ";
     cout<<endl;  

}
时间: 2024-10-07 14:53:40

Binary Tree Right Side View--LeetCode的相关文章

LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 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. LeetCode19

【LeetCode】199. Binary Tree Right Side View

Binary Tree Right Side View 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 <--- Y

Flatten Binary Tree to Linked List leetcode java

题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 题解:如hint所给出,这道题就是使用先序遍历,遍历到的值作为新的右孩子存起来,左孩子变为空.注意的是,因为右孩子会更新,所以为了递归右子树,要在更新之前提前保存右孩子.整个程序需要维护一个全局变量,保

Binary Tree Maximum Path Sum leetcode java

题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. 题解: 递归求解. 取当前点和左右边加和,当前点的值中最大的作为本层返回值.并在全局维护一个max.使用数组,因为是引用类型.所以在递归过程中可以保存结果. 代码如下: 1

&lt;LeetCode OJ&gt; 199. Binary Tree Right Side View

Total Accepted: 40438 Total Submissions: 117654 Difficulty: Medium 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

LeetCode 199. Binary Tree Right Side View

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> rightSideView

【leetcode】Binary Tree Right Side View(middle)

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

[Leetcode] Binary Tree Right Side View

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

8.8 LeetCode 199 Binary Tree Right Side View

Question link 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 retu

LeetCode OJ Binary Tree Right Side View

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