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

解题思路

层次遍历法,找出每一层最右端的结点。

实现代码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;
时间: 2024-10-26 02:37:36

[LeetCode] Binary Tree Right Side View的相关文章

[LeetCode]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. For example: Given the following binary tree, 1 <- / \ 2 3 <- \ \ 5 4 <- You should return [1, 3, 4]

[LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

目录:1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次遍历从低往高输出 BFS 3.Maximum Depth of Binary Tree - 求二叉树的深度 DFS4.Balanced Binary Tree - 判断平衡二叉树 DFS5.Path Sum - 二叉树路径求和判断DFS 题目概述:Given a binary tree, return

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 <

[LeetCode] Binary Tree Paths - 二叉树基础系列题目

目录:1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Paths 题目概述:Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1-

LeetCode: Binary Tree Level Order Traversal 层序遍历二叉树

 Binary Tree Level Order Traversal  题目描述: 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 ord

[LeetCode] Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 递归实现代码 /*********************************************

[LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal

题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

[LeetCode]94.Binary Tree Inorder Traversal

[题目] Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means? &

[LeetCode]144.Binary Tree Preorder Traversal

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