Binary Tree的前、中、后序遍历(recursion)

本文详细介绍了二叉树的前序、中序和后序遍历算法,并提供了递归实现方式。通过具体实例展示了每种遍历方法的运行过程及结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

都是使用recursion的方式进行计算处理。
以后更新非递归版本 == > 自己模拟stack数据结构处理。
前序遍历:
Given a binary tree, return the preorder traversal of its nodes’ values.

Have you met this question in a real interview? Yes
Example
Given:

1

/ \
2 3
/ \
4 5
return [1,2,4,5,3].

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Preorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> array = new ArrayList<>();
        if (root == null) {
            return array;
        }
        ArrayList<Integer> left = preorderTraversal(root.left);
        ArrayList<Integer> right = preorderTraversal(root.right);
        array.add(root.val);
        array.addAll(left);
        array.addAll(right);
        return array;
    }
}

中序遍历Given a binary tree, return the inorder traversal of its nodes’ values.

Have you met this question in a real interview? Yes
Example
Given binary tree {1,#,2,3},

1
\
2
/
3

return [1,3,2].

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Inorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        ArrayList<Integer> right = inorderTraversal(root.right);
        ArrayList<Integer> left = inorderTraversal(root.left);
        result.addAll(left);
        result.add(root.val);
        result.addAll(right);
        return result;
    }
}

后序遍历Given a binary tree, return the postorder traversal of its nodes’ values.

Have you met this question in a real interview? Yes
Example
Given binary tree {1,#,2,3},

1
\
2
/
3

return [3,2,1].

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Postorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        ArrayList<Integer> left = postorderTraversal(root.left);
        ArrayList<Integer> right = postorderTraversal(root.right);
        result.addAll(left);
        result.addAll(right);
        result.add(root.val);
        return result;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值