二叉树的先序、中序和后序遍历(Java实现)

通过牛客网上的题目来复习一下二叉树的先序、中序和后序遍历,并用Java实现

实现二叉树的先序、中序和后序遍历

题目大意:给定一个二叉树的根结点,分别进行先序、中序和后序遍历,将遍历得到的序列分别放在三个一维数组中,然后返回三个一维数组组成的二维数组(可类比C语言的实现来理解):

import java.util.*;

/*
* public class TreeNode {
*   int val = 0;
*   TreeNode left = null;
*   TreeNode right = null;
* }
*/

public class Solution {
    /**
     *
     * @param root TreeNode类 the root of binary tree
     * @return int整型二维数组
     */
    public int[][] threeOrders (TreeNode root) {
        // write code here
        int[][] result = new int[3][];
        List<Integer> pre = new ArrayList<>();
        List<Integer> in = new ArrayList<>();
        List<Integer> post = new ArrayList<>();
        preOrder(root, pre);
        inOrder(root, in);
        postOrder(root, post);
        result[0] = new int[pre.size()];
        for (int i = 0; i < result[0].length; i++) {
            result[0][i] = pre.get(i);
        }
        result[1] = new int[in.size()];
        for (int i = 0; i < result[1].length; i++) {
            result[1][i] = in.get(i);
        }
        result[2] = new int[post.size()];
        for (int i = 0; i < result[2].length; i++) {
            result[2][i] = post.get(i);
        }
        return result;
    }
    
    public void preOrder(TreeNode root, List<Integer> pre) {
        if (root == null) {
            return ;
        }
        pre.add(root.val);
        preOrder(root.left, pre);
        preOrder(root.right, pre);
    }
    
    public void inOrder(TreeNode root, List<Integer> in) {
        if (root == null) {
            return ;
        }
        inOrder(root.left, in);
        in.add(root.val);
        inOrder(root.right, in);
    }
    
    public void postOrder(TreeNode root, List<Integer> post) {
        if (root == null) {
            return ;
        }
        postOrder(root.left, post);
        postOrder(root.right, post);
        post.add(root.val);
    }
}

​对上面的写法的优化(在一个方法中同时实现了前、中、后三种遍历,其中的求结点个数可以记一下):

import java.util.*;

/*
* public class TreeNode {
*   int val = 0;
*   TreeNode left = null;
*   TreeNode right = null;
* }
*/

public class Solution {
    /**
     *
     * @param root TreeNode类 the root of binary tree
     * @return int整型二维数组
     */
    int pre = 0;
    int in = 0;
    int post = 0;
    
    public int[][] threeOrders (TreeNode root) {
        // write code here
        int count = nodeCount(root);
        int[][] result = new int[3][count];
        travel(root, result);
        return result;
    }
    // 求出二叉树中结点的总数
    public int nodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return nodeCount(root.left) + nodeCount(root.right) + 1;
    }
    // 三种遍历同时实现
    public void travel(TreeNode root, int[][] result) {
        if (root == null) {
            return ;
        }
        result[0][pre++] = root.val;
        travel(root.left, result);
        result[1][in++] = root.val;
        travel(root.right, result);
        result[2][post++] = root.val;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值