给定一个二叉树的根节点 root
,返回 它的 中序 遍历 。
示例 1:
输入:root = [1,null,2,3] 输出:[1,3,2]
示例 2:
输入:root = [] 输出:[]
示例 3:
输入:root = [1] 输出:[1]
提示:
- 树中节点数目在范围
[0, 100]
内 -100 <= Node.val <= 100
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
/**
* 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> list = new ArrayList<>();
treeNode(list, root);
return list;
}
public void treeNode(List<Integer> list, TreeNode root) {
if (root != null) {
if (root.left != null) {
treeNode(list, root.left);
}
list.add(root.val);
if (root.right != null) {
treeNode(list, root.right);
}
}
}
}
标签:遍历,TreeNode,val,中序,list,right,二叉树,root,left
From: https://blog.csdn.net/linsa_pursuer/article/details/143034124