做网站编辑是不是也要做推广,广州企业黄页大全,西安哪里可以做网站,在线玩的游戏网站94. 二叉树的中序遍历给定一个二叉树的根节点 root #xff0c;返回 它的 中序 遍历 。这题都写不出来#xff0c;废了 以前学数据结构的时候#xff0c;书上老师写伪代码#xff0c;于是递归版的遍历在伪代码中看起来就很简单。但是怎么输出这个list呢#xff0c;其实是另…94. 二叉树的中序遍历给定一个二叉树的根节点 root 返回 它的 中序 遍历 。这题都写不出来废了 以前学数据结构的时候书上老师写伪代码于是递归版的遍历在伪代码中看起来就很简单。但是怎么输出这个list呢其实是另外写了一个方法来传参的。确实是递归学得太差了见一题背一题的感觉。
官方解法
class Solution {public ListInteger inorderTraversal(TreeNode root) {ListInteger res new ArrayListInteger();inorder(root, res);return res;}public void inorder(TreeNode root, ListInteger 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
著作权归作者所有。商业转载请联系作者获得授权非商业转载请注明出处。