Leetcode 199 binary Tree Right Side view two fork TreeView

Source: Internet
Author: User
Original title Address

https://leetcode.com/problems/binary-tree-right-side-view/ Topic Description

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.
Given a binary tree, imagine that you look from the right side of the tree and return to the nodes you can see from the top down.

For example:
For example:

Given The following binary tree,
Give the following two-fork tree

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

You should return [1, 3, 4].
You need to return [1, 3, 4]. Thinking of solving problems

The first thing to be clear is that the nodes you see are the rightmost nodes in each layer, and all we have to do is to follow the sequence to find the rightmost node of each layer and return it. Then for the key points of the sequence, we can consider the similar idea of sequence traversal. Queues are required in sequence traversal, and we consider using queues here as well. But unlike the sequence traversal, we need to be very explicit about separating different layers to preserve the last node of each layer. We can use two queues, one queue to store one layer of nodes, the other to store the next layer of nodes, so that the nodes of different layers are separated. Code CPP

class solution {public:/** gets the rightmost node of each layer */vector<int> Rightsideview (treenode* Roo
        T) {/* Two queues alternately used to get the element at the tail of the team */vector<int> ret;

        Queue<treenode*> queues[2];

        /* Initialize, put the first layer node in the first queue */if (root = NULL) Queues[0].push (root);
        /* Alternate queue, one queue stores one layer of nodes * Another queue stores the next layer of nodes (or previous nodes) */int i = 0, j = 1, tmp;        
        TreeNode *p; while (!queues[0].empty () | |!queues[1].empty ()) {while (!queues[i].empty ()) {p = queues[i].
                Front ();
                Queues[i].pop ();
                if (p->left) Queues[j].push (p->left);
                if (p->right) Queues[j].push (p->right);
            TMP = p->val; }//After traversing a layer node, TMP is the value of the rightmost node of the layer Ret.push_back (TMP); Add to result set I = (i + 1)% 2;
        The role of swapping two queues J = (j + 1)% 2;
    } return ret; }
};

Full Code Https://github.com/Orange1991/leetcode/blob/master/199/cpp/main.cpp

2015/8/5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.