算法小练——二叉树的中序遍历


title: 算法小练——二叉树的中序遍历
categories:

  • Algorithms
    tags:
  • medium
    abbrlink: 2834101691
    date: 2020-01-11 16:58:30

二叉树的中序遍历

给定一个二叉树,返回它的中序 遍历。

示例

输入: [1,null,2,3]
   1
    \
     2
    /
   3

输出: [1,3,2]

代码

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
    List<Integer> list = new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root!=null){
            inorderTraversal(root.left);
            list.add(root.val);
            inorderTraversal(root.right);
        }
        return list;
    }
}
全部评论

相关推荐

05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务