【Binary Tree Right Side View 】cpp

题目:

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

Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.

代码:

/**
 * 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> ret;
            queue<TreeNode*> curr;
            queue<TreeNode*> next;
            if (root) { curr.push(root); ret.push_back(root->val); }
            while ( !curr.empty() )
            {
                while ( !curr.empty() )
                {
                    TreeNode* tmp = curr.front();
                    curr.pop();
                    if ( tmp->left ) next.push(tmp->left);
                    if ( tmp->right ) next.push(tmp->right);
                }
                if (!next.empty()) ret.push_back(next.back()->val);
                swap(next,curr);
            }
            return ret;
    }
};

tips:

BFS算法,level order traversal binary tree

每次保留每一个level的最右一个元素的值。

时间: 2024-10-10 06:03:48

【Binary Tree Right Side View 】cpp的相关文章

【Binary Tree Post order Traversal】cpp

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 代码: stack 1: /** * Definition for a binar

【Binary Tree Level Order Traversal】cpp

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 代码

【Binary Tree Maximum Path Sum】cpp

题目: 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. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode

【Flatten Binary Tree to Linked List】cpp

题目: 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 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNo

【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

【LeetCode-面试算法经典-Java实现】【110-Balanced Binary Tree(平衡二叉树)】

[110-Balanced Binary Tree(平衡二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of eve

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】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

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