码迷,mamicode.com
首页 > 其他好文 > 详细

94. Binary Tree Inorder Traversal(inorder ) ***(to be continue)easy

时间:2018-07-09 14:12:07      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:input   span   int   des   continue   ons   val   for   def   

Given a binary tree, return the inorder traversal of its nodes‘ values.

Example:

Input: [1,null,2,3]
   1
         2
    /
   3

Output: [1,3,2]

Follow up: Recursive solution is trivial, could you do it iteratively?

 Recursive solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<Integer> res = new ArrayList<Integer>();
    public List<Integer> inorderTraversal(TreeNode root) {
        //inorder traveral : left root right
        inorder( root);
        return res;
    }
    void inorder(TreeNode node){
        if(node==null) return;
        inorder(node.left);
        res.add(node.val);
        inorder(node.right);
        
    }
}

 

follow up questions

 

 

 

 

94. Binary Tree Inorder Traversal(inorder ) ***(to be continue)easy

标签:input   span   int   des   continue   ons   val   for   def   

原文地址:https://www.cnblogs.com/stiles/p/leetcode94.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!