题解 | #二叉树的中序遍历#

二叉树的中序遍历

https://www.nowcoder.com/practice/0bf071c135e64ee2a027783b80bf781d

package com.hhdd.数据结构.树;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huanghedidi
 * @Date 2022/7/24 0:21
 */
public class 二叉树的中序遍历 {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode node1 = new TreeNode(2);
        TreeNode node2 = new TreeNode(3);
        root.left = node1;
        node1.right = node2;
        int[] res = inorderTraversal(root);
        System.out.println(res);
    }

    public static int[] inorderTraversal(TreeNode root) {
        // write code here
        List<Integer> res = new ArrayList<>();
        inorer(root, res);

        int[] resArr = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            resArr[i] = res.get(i);
        }
        return resArr;
    }

    public static void inorer(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        inorer(root.left, res);
        // 放入数组
        res.add(root.val);
        inorer(root.right, res);
    }

}

全部评论

相关推荐

点赞 评论 收藏
分享
09-09 21:23
门头沟学院 Java
程序员牛肉:小牛肉来也! 主要就是没有实习经历。因为你的投递方向肯定是中小厂。在小厂中,很少会有公司愿意花钱培养你。因此会更加青睐有实习的同学。再加上你的学历比较差一点,所以找不到是正常的。 跟简历项目啥的已经没有大关系了,就是差一份实习。秋招和日常实习一起投递吧。
点赞 评论 收藏
分享
看网上说华为1145每个人都会收到,请问华为1145是什么东西
老黑奴:面试如果通过当天晚上11:45会收到面试反馈邮件
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务