나는이 코드의 대부분을 내 교수가 받았다. 우리는 이진 트리의 높이를 계산하기위한 재귀 적 방법을 코딩하고 테스트하도록 요청 받았다. 여기 내 등급 클래스객체로 이진 트리의 높이를 얻는다
public int height(TreeNode root)
{
if(root == null)
{
return 0;
}
else
{
return 1 + Math.max(height(root.lc),
height(root.rc));
}
}
public class MainBinaryTreeWithLNRTraversal
{
public static void main(String[] arg
{
BinaryTreeWithLNRTraversal t = new BinaryTreeWithLNRTraversal();
Listing l;
Listing l1 = new Listing("Ann", "1st Avenue", "111 1111");
Listing l2 = new Listing("Bill", "2nd Avenue", "222 2222");
Listing l3 = new Listing("Carol", "3rd Avenue", "333 3333");
Listing l4 = new Listing("Mike", "4th Avenue", "444 4444");
Listing l5 = new Listing("Pat", "5th Avenue", "555 5555");
Listing l6 = new Listing("Sally", "6th Avenue", "666 6666");
Listing l7 = new Listing("Ted", "7th Avenue", "777 7777");
Listing l8 = new Listing("Vick", "8th Avenue", "888 8888");
Listing l9 = new Listing("Will", "9th Avenue", "999 9999");
Listing l10 = new Listing("Zack", "11th Avenue", "101 0101");
Listing l11 = new Listing("Zeek", "12th Avenue", "121 2121");
System.out.println("Tyler Hansen \n");
// insert the nodes
t.insert(l9);
t.insert(l7);
t.insert(l10);
t.insert(l2);
t.insert(l8);
t.insert(l1);
t.insert(l4);
t.insert(l3);
t.insert(l6);
t.insert(l5);
//Output all the nodes in NLR left tree then right tree order
t.showAll();
t.height();
}
}
나는 무엇을 높이 괄호에 넣어야할지 모른다. 어떤 도움이나 힌트를 주시면 대단히 감사하겠습니다. 필요한 경우 더 많은 코드를 제공 할 수 있습니다.