1. 程式人生 > >LeetCode 199 Binary Tree Right Side View(二叉樹層序遍歷)

LeetCode 199 Binary Tree Right Side View(二叉樹層序遍歷)

Given a binary tree, imagine yourself standing on therightside 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]

.

題目大意:給出一棵二叉樹,假設你現在站在它的右側,輸出你看到的節點(即二叉樹的右檢視)。

解題思路:二叉樹層序遍歷,輸出每一層的最後一個節點即可。因為層序遍歷有遞迴和非遞迴兩種寫法,所以本題也給出了兩種解法。

程式碼如下:

/**
 * 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) {
        // levelOrderTraversal(root, ans);
        _levelOrderTraversal(root, 0, ans);
        return ans;
    }
private:
    queue<TreeNode*> que;
    vector<int> ans;
    //非遞迴版本
    void levelOrderTraversal(TreeNode* root, vector<int>& ans) {
        if(root == nullptr) return ;
        int cur = 0, last, val;
        TreeNode* tmp;
        que.push(root);
        
        while(cur < que.size()) {
            last = que.size();
            while(cur < last) {
                tmp = que.front();
                que.pop();
                if(tmp->left) que.push(tmp->left);
                if(tmp->right) que.push(tmp->right);
                cur++;
            }
            ans.push_back(tmp->val);
            cur = 0;
        }
    }
    //遞迴版本
    void _levelOrderTraversal(TreeNode* root, int depth, vector<int>& ans) {
        if(root == nullptr) return ;
        if(depth >= ans.size()) ans.push_back(root->val);
        _levelOrderTraversal(root->right, depth + 1, ans);
        _levelOrderTraversal(root->left, depth + 1, ans);
    }
};

相關推薦

LeetCode 199 Binary Tree Right Side View

Given a binary tree, imagine yourself standing on therightside of it, return the values of the node

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: Gi

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

LeetCode: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 b

[LintCode] Binary Tree Level Order Traversal的層次

描述 給出一棵二叉樹,返回其節點值的層次遍歷(逐層從左往右訪問) 樣例 給一棵二叉樹 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分層遍歷結果:

[LeetCode] Binary Tree Level Order Traversal II

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 ex

劍指Offer——:把列印成多行

對於二叉樹的最好的解決辦法就是遞迴。遍歷方法無外乎先序遍歷,中序遍歷,後序遍歷方法以及層序遍歷方法。這裡給大家安利一個關於樹的面試題的連結,博主walkinginthewind比較全面且詳細的介紹了二叉樹相關的面試題:對於層序遍歷,最好的方法就是用佇列記錄遍歷節點的值,按層列

LeetCode-面試算法經典-Java實現】【107-Binary Tree Level Order Traversal IIII

lin -m length ret itl pub util 實現類 markdown 【107-Binary Tree Level Order Traversal II(二叉樹層序遍歷II)】 【LeetCode-面試算法經典-Java實現】【全

LeetCode 124. Binary Tree Maximum Path Sum最大路徑和

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node

LeetCode | Binary Tree Level Order Traversal IIII

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

LeetCode 144 Binary Tree Preorder Traversal

Given a binary tree, return thepreordertraversal of its nodes' values. For example: Given binary tr

LeetCode 107.Binary Tree Level Order Traversal II (的層次 II)

題目描述: 給定一個二叉樹,返回其節點值自底向上的層次遍歷。 (即按從葉子節點所在層到根節點所在的層,逐層從左向右遍歷) 例如: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7

[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 o

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

[leetcode]199. Binary Tree Right Side View

[leetcode]199. Binary Tree Right Side View Analysis 好冷鴨—— [每天刷題並不難0.0] Given a binary tree, imagine yourself standing on the right

LeetCode 199.Binary Tree Right Side View (的右檢視)

題目描述: 給定一棵二叉樹,想象自己站在它的右側,按照從頂部到底部的順序,返回從右側所能看到的節點值。 示例: 輸入: [1,2,3,null,5,null,4] 輸出: [1, 3, 4] 解釋: 1 <--- / \ 2

#Leetcode# 199. Binary Tree Right Side View

https://leetcode.com/problems/binary-tree-right-side-view/   Given a binary tree, imagine yourself standing on the right side of it, retur

JavaScript刷LeetCode -- 199. Binary Tree Right Side View [Medium]

一、題目  &emmsp;Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from

199. Binary Tree Right Side ViewTree

連結:https://leetcode.com/problems/binary-tree-right-side-view/ 題目:返回每一層的最右節點。 思路:層次遍歷,每次統計當前層的節點個數(cur_low)和下一層的節點個數(next_low),當cur_low ==1 時

[LeetCode 199] Binary Tree Right Side View (遞迴的

題目內容 199 Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, return the values of