Java two fork tree traversal right view-leetcode199

Source: Internet
Author: User

The topics are as follows:

The example given in the topic is not very good, it is easy to make people misunderstand the right node to continue to access the good, but the title is not so.

Change to popular meaning: traverse the binary tree by layer and output the most right end node of each layer.

This is understood when a binary tree sequence traversal problem, with a queue to deal with, but the question is how to identify the most right side of each layer node, I think for a long time, the last way to figure out is to use a marker, such as the above example:

Q Team column, F for the tag node, right for the record's best-left node.

Q:1 Flag right:{}

Q:flag 2 3 encounters a marker bit so move the marker bit and save the team header popup data as follows

Q:2 3 Flag Right:{1}

Q:3 Flag 5 Right:{1}

Q:flag 5 4 encounters a marker bit so move the marker bit and save the team header popup data as follows

Q:5 4 flag Right:{1 3}

Q:4 Flag Right:{1 3}

Q:flag encounters a marker bit so move the marker bit and save the data from the team header as follows

Q:flag right:{1 3 4}

At this point, the queue element is found only 1, exit loop return results

The code is as follows:

     Public classTreeNode {intVal;        TreeNode left;        TreeNode right; TreeNode (intx) {val=x; }    }     PublicList<integer>Rightsideview (TreeNode root) {List<Integer> right =NewArraylist<integer>(); if(Root = =NULL)            returnRight ; Queue<TreeNode> q =NewLinkedlist<treenode>(); TreeNode P=Root; TreeNode Flag=NewTreeNode (-99999999);        Q.add (P);        Q.add (flag);  while(Q.size ()! = 1) {p=Q.poll (); if(P.left! =NULL) Q.add (p.left); if(P.right! =NULL) Q.add (p.right); if(Q.peek (). val = =-99999999) {right.add (p.val);                Q.poll ();            Q.add (flag); }        }        returnRight ; }

Here I mark bit began to use-1, later depressed the discovery test concentrated node element has-1, changed to now this, passed.

In addition to the online review of other people's solution, there will be a layer of code all access, and then go to the next layer of elements to find the right end of each layer node, the code is as follows:

1 /**2 * Definition for binary tree3 * struct TreeNode {4 * int val;5 * TreeNode *left;6 * TreeNode *right;7 * TreeNode (int x): Val (x), left (null), right (null) {}8  * };9  */Ten classSolution { One  Public: Avector<int> Rightsideview (TreeNode *root) { -vector<int>Res; -         if(!root)returnRes; theQueue<treenode*>Q; - Q.push (root); -          while(!Q.empty ()) { -Res.push_back (Q.back ()val); +             intSize =q.size (); -              for(inti =0; i < size; ++i) { +TreeNode *node =Q.front (); A Q.pop (); at                 if(node->left) Q.push (node->Left ); -                 if(node->right) Q.push (node->Right ); -             } -         } -         returnRes; -     } in};

C + + written, both ways can be

Java two fork tree traversal right view-leetcode199

Related Article

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.