2017-03-14 5 views
0

バイナリツリーの "左側のビュー"(ツリーを左側から見たときに見えるノード)を印刷する関数を記述します。例えばバイナリツリーの左側のビューを印刷する

、次のツリーの左側のビューは1 2 4 8

  1 
    / \ 
    2  3 
/ \ /\ 
    4  5 6 7 
        \ 
        8 

である私は他の人のソリューションを見てみたいと思います。

+1

あなたの解決策は何ですか? –

+1

私は本からアルゴリズムの実装を追加しました。 – Aaron

答えて

0

私のソリューションは再帰を使用します。 (C#)

private static int globalDepth = 0; 
    public static void PrintLeftSide (Node root, int localDepth) 
    { 
     if (root == null) 
     { 
      return; 
     } 
     if (localDepth > globalDepth) 
     { 
      Console.WriteLine(root.Value); 
      globalDepth++; 
     } 
     PrintLeftSide(root.Left, localDepth + 1); 
     PrintLeftSide(root.Right, localDepth + 1); 
    } 
関連する問題