94. Binary Tree Inorder Traversal

Source: Internet
Author: User

    /** 94.      Binary Tree inorder Traversal * 2015.11.21 by Mingyang * With the same DFS as preorder, but every time pop comes out in the small books, that is, in the list inside the sequence traversal iterative solution * Use the stack to add all the left children of the root node to the stack, then output the top element of the stack, and then handle the right subtree of the top element of the stack .*/     PublicList<integer>inordertraversal (TreeNode root) {List<Integer> list =NewArraylist<integer>(); if(Root = =NULL)            returnlist; Stack<TreeNode> stack =NewStack<treenode>(); TreeNode P=Root;  while(!stack.isempty () | | P! =NULL) {            if(P! =NULL) {Stack.push (P); P=P.left; } Else{TreeNode T=Stack.pop ();                List.add (T.val); P=T.right; }        }        returnlist; }     PublicList<integer>inorderTraversal1 (TreeNode root) {List<Integer> res =NewArraylist<integer>();        DFS (root, res); returnRes; }     Public voidDFS (TreeNode root, list<integer>Res) {        if(Root = =NULL)            return;        DFS (Root.left, res);        Res.add (Root.val);    DFS (Root.right, res); }

94. Binary Tree Inorder Traversal

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.