给定二进制搜索树的根节点,我正在尝试创建一个递归搜索,其中找到给定最大和最小范围内的所有节点但是访问量最少 .
所以基本上这个问题的设置将是(我认为):
public Node finder(Node root,int max,int min){};
创建一个递归定义为的函数“check”
check(Node x){ if(x.right.elm()>min&&x.right.elm()<max){ check(x.right); } else if(x.right.elm()>max){ check(x.right.left); else if(x.right.elm()<min){ check(x.right.right); } if(x.left.elm()>min&&x.left.elm()<max){ check(x.left); } else if(x.left.elm()>max){ check(x.left.left); else if(x.left.elm()<min){ check(x.left.right); } }
或者,为了更简单的代码,您可以创建两个函数check_right和check_left .
1 回答
创建一个递归定义为的函数“check”
或者,为了更简单的代码,您可以创建两个函数check_right和check_left .