当前位置: 首页 > 知识库问答 >
问题:

我怎么走这个递归二叉树问题?

陈修诚
2023-03-14

这是该问题的有效解决方案:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return helper(nums, 0, nums.length-1);
    }

    public TreeNode helper(int[] nums, int low, int high){
        if (high < low){ return null; }
        //find max element's index
        int maxIndex = getMaxIndex(nums, low, high);

        //construct root node
        TreeNode root = new TreeNode(nums[maxIndex]);

        //generate left subtree from the left part of subarray
        root.left = helper(nums, low, maxIndex-1);

        //generate right sub tree from the right part of subarray
        root.right = helper(nums, maxIndex+1, high);


        return root; 
    }

    public int getMaxIndex(int[] nums, int low, int high){
        int maxIndex = low;
        for (int i = low+1; i <= high; i++){
            if (nums[i] > nums[maxIndex]){
                maxIndex = i;
            }
        }
        return maxIndex;
    }
}

有人能帮我解释一下这个问题和所有的递归调用吗?现在我不明白解决方案是如何构建树节点的。我目前正在这样处理这个问题。

>

  • ConstructMaximumBinaryTree(int[]nums)

    int maxIndex=getMaxIndex(nums,0,5)因此maxIndex=3。

    树节点根=6。

    root.left=helper(nums,0,2),因此maxIndex=0。

    树节点根=3。

    root.left=helper(nums,0,-1),它触发基大小写并返回null。

    第6步之后我就迷路了。在步骤6返回null之后,我是否继续到root.right=helper(nums,maxindex+1,high)?如果是的话,maxIndex和high会是多少?接下来的步骤是什么?

  • 共有1个答案

    万俟沛
    2023-03-14

    简单的答案是肯定的,您移动到root.right=helper(nums,maxIndex+1,high),其中maxIndex=0且high=2,因此root.right=helper(nums,1,2)。

    这些步骤将是:

    1. ConstructMaximumBinaryTree(int[]nums)
    2. int maxIndex=getMaxIndex(nums,0,5)所以maxIndex=3。
    3. 树节点根=6。
    4. root.left=helper(nums,0,2)因此maxIndex=0。
    5. 树节点根=3。
    6. root.left=helper(nums,0,-1),它触发基本大小写并返回NULL。
    7. 对于root=3,我们继续使用右侧子树,因此root.right=helper(nums,1,2),maxIndex=1。
    8. 树节点根=2。
    9. root.left=helper(nums,1,0),它触发基本大小写并返回NULL。
    10. 对于root=2,我们继续使用右侧子树,因此root.right=helper(nums,2,2),maxIndex=2。
    11. 树节点根=1。
    12. 现在左和右都返回null,我们返回根=6的右子树。
     类似资料:
    • 主要方法: 如果您需要类'BinaryNode',请询问,我会张贴它,我不想用代码交换这个问题... 输入: null null 我不明白为什么节点'2'和'3'返回时左值和右值为null。

    • 我正在研究二叉树。我在网上看到了一个遍历整个二叉树的代码。这是我得到的代码:“” “‘ 我不明白的是这个函数如何打印正确的孩子?根据代码每次调用函数时,左子被打印出来。代码永远不会到达正确的孩子。

    • 我尝试使用递归遍历二叉树。每个树要么有两个子树,要么没有子树(即,为子树保留的字段==无) 我想将每个分支(即两个子节点==无的每个节点)的最后叶子添加到一个列表中,并返回该列表。我使用“搜索”功能和助手“搜索库”功能来实现这一点。 通过调试器,我看到“search”函数中的列表确实包含我希望它显示的元素。但是,当它在search_base函数中返回时,结果似乎是一个空列表。 我非常困惑,如果有任

    • 由于它是一种递归方法,我无法弄清楚如何使用这些stg参数来存储树元素数据。我希望将stg保留在那里,以便学习如何将字符串数据存储在递归方法中。我该怎么做呢?(基本上我想摆脱诱惑1) 编辑:我尝试了stg+=root.getElement()+“”;带返回STG;但这并不奏效 输出示例“树的顺序遍历:1 2 3 X Y Z X Y Z”

    • 我正试图建立一个二叉树来执行以下操作: > 如果我写Y,它将打印左边的子对象。如果我写N,它将打印正确的子对象。 如果它只是一个叶节点,它将只写下答案。

    • 我试图为我的BST做一个递归加法。public add方法接受一个int参数,private方法接受相同的int和一个节点。这是我目前掌握的代码 我经常得到空指针,也尝试过调试,但我的逻辑中有一些我看不到的缺陷。