如何在这个递归方法中只使用一个返回值。

huangapple go评论100阅读模式
英文:

how to use only 1 return in this recursive method

问题

  1. // 有多少个节点恰好有一个非空子节点
  2. public int stickCt() {
  3. return stickCt(root);
  4. }
  5. private int stickCt(StringNode t) {
  6. int count = 0;
  7. if (t == null)
  8. return 0;
  9. else if ((t.getLeft() == null && t.getRight() != null) || (t.getLeft() != null && t.getRight() == null))
  10. count++;
  11. count = count + stickCt(t.getLeft()) + stickCt(t.getRight());
  12. return count;
  13. }
英文:

I'm finishing up the easy portion project and my professor specified that he only wants methods with at maximum 1 return. I can't seem to figure out how to make this work correctly with only 1 return though. For context, I am finding how many nodes in a tree have exactly 1 non-null children.

  1. // How many nodes have exactly 1 non-null children
  2. public int stickCt() {
  3. return stickCt(root);
  4. }
  5. private int stickCt(StringNode t) {
  6. int count = 0;
  7. if (t == null)
  8. return 0;
  9. else if ((t.getLeft() == null && t.getRight() != null) || (t.getLeft() != null & t.getRight() == null))
  10. count++;
  11. count = count + stickCt(t.getLeft()) + stickCt(t.getRight());
  12. return count;
  13. }

答案1

得分: 1

以下是翻译好的部分:

  1. private int stickCt(StringNode t) {
  2. int count = 0;
  3. if (t != null) {
  4. if ((t.getLeft() == null && t.getRight() != null) || (t.getLeft() != null && t.getRight() == null))
  5. count++;
  6. count += stickCt(t.getLeft()) + stickCt(t.getRight());
  7. }
  8. return count;
  9. }
  10. 然而在这种情况下我发现您使用两个返回语句的表述更容易理解
英文:

Something like this:

  1. private int stickCt(StringNode t) {
  2. int count = 0;
  3. if (t != null) {
  4. if ((t.getLeft() == null && t.getRight() != null) || (t.getLeft() != null & t.getRight() == null))
  5. count++;
  6. count += stickCt(t.getLeft()) + stickCt(t.getRight());
  7. }
  8. return count;
  9. }

However, in this case I find your formulation with the 2 returns easier to understand.

答案2

得分: 0

使用递归算法的替代方法是使用广度优先搜索(BFS)或深度优先搜索(DFS)算法。

以下是一个BFS方法的示例:

  1. public int stickCt(StringNode root){
  2. int count = 0;
  3. Queue<StringNode> queue = new LinkedList<StringNode>();
  4. queue.add(root);
  5. while (!queue.isEmpty()){
  6. StringNode tempNode = queue.poll();
  7. if (tempNode != null && (tempNode.getLeft() == null && tempNode.getRight() != null) || (tempNode.getLeft() != null && tempNode.getRight() == null)){
  8. count++;
  9. }
  10. /*将左子节点入队*/
  11. if (tempNode.getLeft() != null) {
  12. queue.add(tempNode.getLeft());
  13. }
  14. /*将右子节点入队*/
  15. if (tempNode.getRight() != null) {
  16. queue.add(tempNode.getRight());
  17. }
  18. }
  19. return count;
  20. }

注意:由于您请求只返回翻译的部分,我已经将代码部分翻译为中文。如果您有任何其他问题或需要进一步帮助,请随时提问。

英文:

Instead using recursive algorithm you can use BFS of DFS algorithm.

Here is an example or BSF approach

  1. public int stickCt(StringNode root){
  2. int count = 0;
  3. Queue&lt;StringNode &gt; queue = new LinkedList&lt;StringNode &gt;();
  4. queue.add(root);
  5. while (!queue.isEmpty()){
  6. StringNode tempNode = queue.poll();
  7. if (tempNode != null &amp;&amp; (tempNode.getLeft() == null &amp;&amp; tempNode.getRight() != null) || (tempNode.getLeft() != null &amp;&amp; tempNode.getRight() == null)){
  8. count++;
  9. }
  10. /*Enqueue left child */
  11. if (tempNode.getLeft()!= null) {
  12. queue.add(tempNode.getLeft());
  13. }
  14. /*Enqueue right child */
  15. if (tempNode.getRight()!= null) {
  16. queue.add(tempNode.getRight());
  17. }
  18. }
  19. return count;
  20. }

huangapple
  • 本文由 发表于 2020年10月11日 16:06:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64301848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定