首页 > 其他 > 详细

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

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!