94. 二叉树的中序遍历给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
这题都写不出来,废了
以前学数据结构的时候,书上老师写伪代码,于是递归版的遍历在伪代码中看起来就很简单。但是怎么输出这个list呢,其实是另外写了一个方法来传参的。确实是递归学得太差了,见一题背一题的感觉。
官方解法
class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<Integer>();inorder(root, res);return res;}public void inorder(TreeNode root, List<Integer> res) {if (root == null) {return;}inorder(root.left, res);res.add(root.val);inorder(root.right, res);}
}作者:力扣官方题解
链接:https://leetcode.cn/problems/binary-tree-inorder-traversal/solutions/412886/er-cha-shu-de-zhong-xu-bian-li-by-leetcode-solutio/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。