Advertisement

二叉树遍历面试题

阅读量:

1,二叉树的前序遍历

题目网址:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
针对一棵二叉树结构,要求输出其前序遍历的结果。
所采用的处理方式较为直接,即首先访问当前节点,随后依次对左子树和右子树进行递归处理。

复制代码
    /** * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null){
            return result; //空链表
        }
        //访问根节点
        resul

全部评论 (0)

还没有任何评论哟~