Leecode打卡第二十一天
二叉树的中序遍历
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
主要思想:递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
indorder(root, res);
return res;
}
void indorder(TreeNode root, List<Integer> list){
if(root == null) return;
indorder(root.left, list);
list.add(root.val);
indorder(root.right, list);
}
}