- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习几个小时后的考试,我正在复习一些练习题,但我在递归相关问题上确实遇到了麻烦。我想知道是否有人可以引导我完成这些操作?
使用下面的BinaryTree 类
,创建以下递归
方法:
sum()
– 返回二叉树中所有值的总和。 countGreaterThan(int)
– 返回值大于指定整数的节点数。 isValidSearchTree()
– 如果二叉树是有效的搜索树,则返回 true,否则返回 false。 我了解了如何创建这些函数的基本前提,例如,对于 sum 函数,它通常会类似于:
int sum = 0
for(int i; i<binaryTree.size(); i++){
sum += binaryTree[i]
}
或者类似的东西。但我真的不知道如何将这些概念应用于具有节点的二叉树并使它们也递归?下面给出了我提供的代码。
public class BinaryTree {
private int data;
private BinaryTree leftChild;
private BinaryTree rightChild;
public BinaryTree(int val) {
data = val;
leftChild = null;
rightChild = null;
}
// Get methods
public int getData() { return data; }
public BinaryTree getLeftChild() { return leftChild; }
public BinaryTree getRightChild() { return rightChild; }
// Set methods
public void setData(int val) { data = val; }
public void setLeftChild(BinaryTree left) { leftChild = left; }
public void setRightChild(BinaryTree right) { rightChild = right; }
}
最佳答案
你应该研究recursion
的力量 ,但我已经编写了您要求的方法,所有这些都像魅力一样工作:
/*
* INPUT: Root node
* OUTPUT: Sum of the data of all nodes of tree
*/
public void sum(TreeNode node)
{
if(node == null)
return;
sum(node.leftNode); // recursive call to left subtree of each node
sum += node.data;
sum(node.rightNode); // recursive call to right subtree of each node
}
/*
* INPUT: Root node , threshold value
* OUTPUT: sum of all node's data, that rae greater than "value"
*/
public void countGreaterThan(TreeNode node, int value)
{
if(node == null)
return;
countGreaterThan(node.leftNode,value);
if( node.data > value) // only adds is node.data is greater than given value
sum += node.data;
countGreaterThan(node.rightNode,value);
}
/*
* INPUT: nothing
* OUTPUT: call to its helper function, taking MIN, MAX, and root as input
*/
public boolean isBST()
{
return isBSThelper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
public boolean isBSThelper(TreeNode node, int min, int max)
{
if (node == null) //empty tree is always a BST
return true;
if (node.data < min || node.data > max) //if node breaks the min/max condition
return false;
// recursive call to left subtree and right subtree
return (isBSThelper(node.leftNode, min, node.data-1) && isBSThelper(node.rightNode, node.data+1, max));
}
关于java - 创建给定 BinaryTree 的递归方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55669616/
我玩了一点二叉树并构建了一个菜单,当我单击创建树时,用户可以选择是构建二叉树、向他构建的二叉树插入值还是删除它。树被创建,然后菜单再次出现,现在我想在这棵树中放一个数字,但是在案例中没有设置该变量,每
问题出在有两个参数的insert方法上。函数 insert(char letter, string code) 调用 insert(TreeNode *node, char letter, char
我想以数组的形式返回所有节点的值,但是返回值是错误的。 type TreeNode struct { Left *TreeNode Right *TreeNode Val
我正在学习几个小时后的考试,我正在复习一些练习题,但我在递归相关问题上确实遇到了麻烦。我想知道是否有人可以引导我完成这些操作? 使用下面的BinaryTree 类,创建以下递归方法: sum() –
我的想法是将字符转换为二进制。例如,“f”将为“100”。为了尝试这个示例,我在下面的代码中创建了扩展二叉树: package ej2; public class PrincipalCod { /**
我有个问题...如果我有这样的二叉树: typedef struct { char* text; char* line; struct Node* left; struct N
我想在我的参数化二叉树类中添加双向迭代器(如 std::set 导出的迭代器),但我无法想出任何算法。 二叉树节点的简单结构是,它包含三个指针,left,right,parent: 最佳答案 对于给定
我想在图的上部绘制二叉树,并在第二部分(底部)制作第二个二叉树。下面是一些示例代码,显示树的图完全忽略了 par() 设置的分区选项。 library("party") ### regression
我目前正在学习数据结构和算法类(class)。上节课,我的教授在黑板上贴了一些代码来解释如何编写 size() ,height() ,和left()/right()教我们在测验中保持两种平衡的方法。
我是一名优秀的程序员,十分优秀!