【问题标题】:How to get all non-leaf nodes in a binary search tree using recursion?如何使用递归获取二叉搜索树中的所有非叶节点?
【发布时间】:2021-02-13 14:57:54
【问题描述】:

我正在尝试使用递归来获取二叉搜索树的所有非叶节点。我可以计算非叶节点,但是如何返回它们..?

int countNonLeafNodes(Node root)
{ 
    if (root == null || (root.left == null &&  
                         root.right == null)) 
        return 0; 
    return 1 + countNonLeafNodes(root.left) +  
               countNonLeafNodes(root.right); 
}

【问题讨论】:

    标签: java recursion binary-tree


    【解决方案1】:

    您可以尝试将其作为数组返回。所以它本质上会返回 countNonLeafNodes(root.left) 数组,并附加 countNonLeafNodes(root.right) 数组。

    【讨论】:

      猜你喜欢
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 2016-09-15
      • 2018-03-19
      相关资源
      最近更新 更多