Binary Tree Inorder Traversal

来源:互联网 发布:万网域名控制面板登录 编辑:程序博客网 时间:2024/06/07 01:50

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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

中序遍历 代码如下:

public class Solution {    public List<Integer> inorderTraversal(TreeNode root) {        List<Integer> res=new ArrayList<Integer>();        if(root==null) return res;        if(root.left==null&&root.right==null) {            res.add(root.val);            return res;        }       res.addAll(inorderTraversal(root.left));       res.add(root.val);       res.addAll(inorderTraversal(root.right));       return res;    }}


0 0
原创粉丝点击