当前位置:   article > 正文

华为OD面试题:数组构建二叉树_华为面试二叉树

华为面试二叉树

华为OD面试题:数组构建二叉树

华为面试手写题目:

根据一个数组,构建一颗二叉树,并且使用中序遍历验证输出

例如:输入数组 [1, 2, 3, null, null, 4, 5]

则构建的二叉树为

    1
   / \
 2   3
    /  \
   4   5
  • 1
  • 2
  • 3
  • 4
  • 5

然后对其进行中序遍历输出:[2, 1, 4, 3, 5]

题解如下:

第一步:根据数组创建一颗二叉树

/**
 * 根据数组创建一颗二叉树
 */
public static TreeNode constructTree(Integer[] nums) {
    if (nums == null || nums.length == 0 || nums[0] == null) {
        return null;
    }
    int index = 0;
    int length = nums.length;

    TreeNode root = new TreeNode(nums[0]);
    // 定义一个队列存储节点
    Deque<TreeNode> nodeQueue = new LinkedList<>();
    // 将root节点添加到头节点
    nodeQueue.offer(root);
    TreeNode currNode;
    while (!nodeQueue.isEmpty()) {
        // 获取并移除队列的头节点
        currNode = nodeQueue.poll();
        
        index++;
        if (index >= length) {
            return root;
        }
        if (nums[index] != null) {
            // 左子树赋值
            currNode.left = new TreeNode(nums[index]);
            // 将左子树的节点添加至队列尾部
            nodeQueue.offer(currNode.left);
        }
        
        index++;
        if (index >= length) {
            return root;
        }
        if (nums[index] != null) {
            // 右子树赋值
            currNode.right = new TreeNode(nums[index]);
            // 将右子树添加至队列的尾部
            nodeQueue.offer(currNode.right);
        }
    }
    return root;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

第二步:对树进行中序遍历

解法一:使用递归算法

/**
 * 二叉树的中序遍历
 */
public static List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> result = new ArrayList<>();
    preorder(root, result);
    return result;
}

private static void preorder(TreeNode node, List<Integer> result) {
    // 终止条件
    if (node == null) {
        return;
    }
    // 中序遍历:左子树 -> 根节点 -> 右子树
    preorder(node.left, result);
    result.add(node.val);
    preorder(node.right, result);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

解法二:使用迭代算法

其实递归算法相当于隐式维护了一个栈,我们可以自己维护一个栈

/**
 * 二叉树的中序遍历(迭代)
 */
public static List<Integer> inorderTraversal(TreeNode root) {
    ArrayList<Integer> result = new ArrayList<>();
    // 定义一个栈
    Stack<TreeNode> stack = new Stack<>();
    // 定义一个指针
    TreeNode currNode = root;
    while (!stack.isEmpty() || currNode != null) {
        while (currNode != null) {
            stack.push(currNode);
            // 左节点
            currNode = currNode.left;
        }
        currNode = stack.pop();
        // 根节点
        result.add(currNode.val);
        // 右节点
        currNode = currNode.right;

    }
    return result;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/470736
推荐阅读
相关标签
  

闽ICP备14008679号