【LeetCode】#94二叉树的中序遍历(Binary Tree Inorder Traversal)
题目描述
给定一个二叉树,返回它的中序 遍历。
示例
输入: [1,null,2,3]
1
2
/
3
输出: [1,3,2]
Description
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]
解法
class Solution{
public List<Integer> inorderTraversal(TreeNode root){
List<Integer> res = new ArrayList<>();
if(root==null){
return res;
}
if(root.left!=null){
res.addAll(inorderTraversal(root.left));
}
res.add(root.val);
if(root.right!=null){
res.addAll(inorderTraversal(root.right));
}
return res;
}
}