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

https://leetcode.com/problems/binary-tree-right-side-view/ 题目位置

其实就是层序遍历,然后记录每一层中节点,最后一个的值

经常做到和层序相关的题目,之前是一个二叉树的Z扫描的问题,和这里基本上才用了一样的数据结构,所以很熟悉,写完就AC。

利用了一个vector存储每一层的元素,然后进行一个简单的递归

如果二叉树中没有对每一行有特别需要的要求,那么就是可以直接利用 队列,如果有要求,也可以在队列中加入一个类似NULL节点表示结束

http://blog.csdn.net/xietingcandice/article/details/44939919

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    vector<int> rightSideView(TreeNode *root) { //其实就是每一行的最后一个节点
        res.clear();
        if(root == NULL)
        {
            return res;
        }
        vector<TreeNode*> node;
        node.push_back(root);
        GetNum(node);
        return res;
    }
    void GetNum(vector<TreeNode*> &node)
    {
        if(node.empty())
        {
            return;
        }
        vector<TreeNode*>pNode;
        //<获取当前层最右边节点的值
        res.push_back(node[node.size()-1]->val);
        //<按顺序获取他的子节点
        for(int i = 0; i < node.size();i++)
        {
            TreeNode * root = node[i];
            if(root->left)
            {
                pNode.push_back(root->left);
            }
            if(root->right)
            {
                pNode.push_back(root->right);
            }
        }
        GetNum(pNode);
    }
};
时间: 2024-11-08 06:33:28

Binary Tree Right Side View 二叉树层序遍历变形的相关文章

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 order traversal as: [ [3], [9,20], [15,7] ] 层序遍历二叉

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

Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List&lt;list&gt;

问题描述: 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,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15

leetcode-Binary Tree Level Order Traversal 二叉树层序遍历

#include<stdio.h> #include<queue> using namespace std; typedef struct BiTree { int val; struct BiTree *lchild; struct BiTree *rchild; }BiTree; void main() { BiTree *root; queue<BiTree *> q; BiTree *t; q.push(root); int size; //核心代码 while

leetCode102. Binary Tree Level Order Traversal 二叉树层次遍历

102. 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,null,null,15,7],     3    /   9  20     /     15   7 re

【LeetCode】Binary Tree Level Order Traversal(层序遍历)

Question 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

Leetcode: 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? 递归解法(C++): /** * Definition for binary tre

leetcode 题解: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 order traversal as: [ [3], [9,20], [15,7] ] co

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example