아무도 이 방법을 찾을 수 있다면 나도 궁금 돕는다구요 재작업 높이를 이진 탐색 트리. 지금까지 내 코드 모양입니다. 그러나 실제 # 39 m, 높이 1 씩 받고 I& 오토메이티드 보다 큽니다. 그러나 내가 분리하십시오 it& # 39 의 보다 +1 from my 반품하십시오 명령문입니다, 실제 높이 1 씩. # 39 랩 머리는요 수립하였습니다 i& 위해 아직도 노력하고, m 이 반복이 영국 서머 타임. 어떤 도움이 많이 될 것이라고 평가했다.
public int findHeight(){
if(this.isEmpty()){
return 0;
}
else{
TreeNode<T> node = root;
return findHeight(node);
}
}
private int findHeight(TreeNode<T> aNode){
int heightLeft = 0;
int heightRight = 0;
if(aNode.left!=null)
heightLeft = findHeight(aNode.left);
if(aNode.right!=null)
heightRight = findHeight(aNode.right);
if(heightLeft > heightRight){
return heightLeft+1;
}
else{
return heightRight+1;
}
}
어떤 일이 벌어질까 in your base case.
", 높이를 진단트리는 디스테이징하는 사이의 경로가 깊은 트리에서 노드를 루트. 한 개의 노드 (루트) 는 높이 (한다고) 진단트리는 zero.". - 위키백과
1, 0 없으면 노드입니다 운영까지도 반환되기를 없습니다. 마지막에 1 추가할 수 있기 때문이다.
그래서 협업공간이 isn& # 39, t, 당신은 어떤 취소창을 아웃해야 노드입니다 복귀하십시오 - 1 (+1.
int findHeight(TreeNode<T> aNode) {
if (aNode == null) {
return -1;
}
int lefth = findHeight(aNode.left);
int righth = findHeight(aNode.right);
if (lefth > righth) {
return lefth + 1;
} else {
return righth + 1;
}
}
이진 탐색 진단트리는 높이를 레이어에는 - 1 에 해당하는 '수'.
구성도를 dell. (http://en.wikipedia.org/wiki/Binary_tree) 참조
그래서 그냥 subtract 좋지만 사용자의 루트에 레벨이네 한 반복
또한, 이 함수를 통해 취급료 nulll 노드입니다 조금 정리할 수 있습니다.
int findHeight(node) {
if (node == null) return 0;
return 1 + max(findHeight(node.left), findHeight(node.right));
}
제 생각에는, 코드에서 조금 간편화된 혜택을 누릴 수 있다. , 포인터입니다 널임 시도중입니다 반복이 아닌 종료하십시오 때 하위 끝날 때 이 현재 포인터입니다 널임. 쓸 수 있는 코드 많이유 간단해진다는. 의사코드의, 정보기술 (it) 의 구성은 다음과 같습니다.
if (node = null)
return 0;
else
left = height(node->left);
right = height(node->right);
return 1 + max(left, right);
이것은 분명히 있지만 검증되지 않은 비교적 정확한:
< pre>; int 핀데하이트 pbs. (Treenode< T>; 양극) { if (null = & 아노드그레프트 &; 아누데스라이트 = null) { return 0; // sanlight 1; 노드입니다 하였더뇨 높이 0 이 없는 것으로 알려졌다. {} else if (null 아노드그레프트 =) 핀데하이트 복귀하십시오 1 + (아누데스라이트); {} else if (null 아누데스라이트 =) 핀데하이트 복귀하십시오 1 + (아노드그레프트); } else { 반품하십시오 1 + max (핀데하이트 (아노드그레프트), 핀데하이트 (아누데스라이트)); } } < /pre>;
S # 39, 간결하고 here& 표현할 수 있기를 바랍니다 정답이 없다.
private int findHeight(TreeNode<T> aNode){
if(aNode == null || (aNode.left == null && aNode.right == null))
return 0;
return Math.max(findHeight(aNode.left), findHeight(aNode.right)) + 1;
}
public void HeightRecursive()
{
Console.WriteLine( HeightHelper(root) );
}
private int HeightHelper(TreeNode node)
{
if (node == null)
{
return -1;
}
else
{
return 1 + Math.Max(HeightHelper(node.LeftNode),HeightHelper(node.RightNode));
}
}
C # 코드입니다. 영국 서머 타임 클래스 내 이 두 가지 방법이 있습니다. 두 가지 방법을 높이 진단트리 계산하십시오 합니다. 이 헤이트헬퍼 계산하십시오 &. 그 하이트레커사이브 인쇄하십시오 주 ().
public int height(){
if(this.root== null) return 0;
int leftDepth = nodeDepth(this.root.left, 1);
int rightDepth = nodeDepth(this.root.right, 1);
int height = leftDepth > rightDepth? leftDepth: rightDepth;
return height;
}
private int nodeDepth(Node node, int startValue){
int nodeDepth = 0;
if(node.left == null && node.right == null) return startValue;
else{
startValue++;
if(node.left!= null){
nodeDepth = nodeDepth(node.left, startValue);
}
if(node.right!= null){
nodeDepth = nodeDepth(node.right, startValue);
}
}
return nodeDepth;
}
정의 위에 주어진 높이 잘못되었습니다. 즉 정의를 심도입니다.
, M 깊이에 있는 진단트리는 " 노드입니다 디스테이징하는 사이의 경로가 나무를요 루트 로 M . 1) 보다 심도 깊은 높이를 진단트리는 노드입니다 나무를요. D 진단트리의 깊이까지의 모든 노드 d 레벨에서만 있다. 루트 노드에서 유일한 레벨 0, 그 깊이는 0.".
정적 변수가 트럼프헤이트 (처음에는 0) 로 설정합니다.
static void 핀데하이트 (노드 node, int count) {
if (node == null) {
return;
}
if ((node.right == null) && (node.left == null)) {
if (tempHeight < count) {
tempHeight = count;
}
}
findHeight(node.left, ++count);
count--; //reduce the height while traversing to a different branch
findHeight(node.right, ++count);
}
이 솔루션을 자바 가상 머신 (jvm) 에 약간 긴 하지만 작동합니다.
public static int getHeight (Node root){
int lheight = 0, rheight = 0;
if(root==null) {
return 0;
}
else {
if(root.left != null) {
lheight = 1 + getHeight(root.left);
System.out.println("lheight" + " " + lheight);
}
if (root.right != null) {
rheight = 1+ getHeight(root.right);
System.out.println("rheight" + " " + rheight);
}
if(root != null && root.left == null && root.right == null) {
lheight += 1;
rheight += 1;
}
}
return Math.max(lheight, rheight);
}
높이 com/go/downloads_kr //function 영국 서머 타임
int height(Node* root) {
if(root == NULL){
return -1;
}
int sum=0;
int rheight = height(root->right);
int lheight = height(root->left);
if(lheight>rheight){
sum = lheight +1;
}
if(rheight > lheight){
sum = rheight + 1;
}
return sum;
}
이 질문에 서로 다른 두 전지전능하심이라 짓궂군요 수 있을 것 같아요
'int 칼체이트 (노드입니다 루트) { if (root = NULL) return 0; int (root->, 왼쪽), l = 칼체이트 int r = 칼체이트 (root->, 오른쪽). if (l>, r) 반품하십시오 l + 1. 다른 반품하십시오 r + 1. } ' 2. 높이 는 총 노드입니다 나무를요 * 한다.
'int 칼크시즈 (노드입니다 * 루트) { if (root = NULL) return 0; 반환 (칼크시즈 (root->, 왼쪽) +1 + 칼크시즈 (root->, 오른쪽)); } '
이 경우 읽는 사람이!!!!
높이가 가장 긴 경로를 정의된 수로 노드입니다 맞추기바인딩 뿌리 노드로부터 잎 노드입니다. 따라서: 루트 노드에서 나무 높이 1 개밖에 없으며 0.
거리를 고려할 때 수준은 루트 노드는 플러스 (+) 1. 따라서: 자식 노드를 루트 켜있을 오른길로 레벨 1, 2 단계 및 드릴링됩니다.
(정보 고객 편의를 위한 데이터 구조: 추상화 및 설계, Java 를 이용하여 두 번째 에디션 의해 엘리엇 B. 코프먼 &. 현재 사용되는 데이터 구조에 관한 책 폴 A. T. 볼프강) - 코스 실로나는 http://support. 콜럼버스 주립 대학교.