Binary Tree inorder Traversal

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source: http://blog.csdn.net/pistolove/article/details/42876657


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 do it iteratively?


Ideas:

(1) Test instructions for the middle sequence traversal binary tree. The traversal order is left-to-root, right.

(2) Considering that recursion is relatively simple, this article uses the idea of recursion to solve, because relatively simple here is not cumbersome, see the code below.

(3) Hope this article is helpful to you.


The algorithm code is implemented as follows:

/** * @author liqq */public list<integer> inordertraversal (TreeNode root) {list<integer> result = new Linkedli St<integer> (); if (root = null) {In_order (result, root.left); Result.add (Root.val); In_order (result, root.right) ;} return result;} private void In_order (list<integer> result, TreeNode Curr) {if (Curr! = null) {In_order (result, curr.left); result.a DD (curr.val); In_order (result, curr.right);}}




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.