-
Notifications
You must be signed in to change notification settings - Fork 0
102. Binary Tree Level Order Traversal #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * 自力解法。10分ほどでAC 時間計算量: O(n), 空間計算量: O(n) n:全要素数 思考プロセスとしては、各階層のものを順にみていくのでBFSが一番直感的だと思ったのでそれで実装。 | ||
| * 階層の区切りにQueueを使った。 | ||
| */ | ||
| public class solution1_1 { | ||
| public List<List<Integer>> levelOrder(TreeNode root) { | ||
| List<List<Integer>> values = new ArrayList<>(); | ||
| if (root == null) { | ||
| return values; | ||
| } | ||
| Queue<TreeNode> nodes = new ArrayDeque<>(); | ||
| nodes.offer(root); | ||
| while (!nodes.isEmpty()) { | ||
| Queue<TreeNode> nodesInNextLevel = new ArrayDeque<>(); | ||
| List<Integer> valuesInNextLevel = new ArrayList<>(); | ||
| while (!nodes.isEmpty()) { | ||
|
Comment on lines
+13
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 初見で同じwhile()が2個あって、意図を区別するまでに戸惑いがありました。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます!そのじてんでのQueueのsizeでやる感じですかね |
||
| TreeNode current = nodes.poll(); | ||
| valuesInNextLevel.add(current.val); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます!つぎの階層での探索になるのでNextかなと思っていたのですが認識相違ありますでしょうか? |
||
| if (current.left != null) { | ||
| nodesInNextLevel.offer(current.left); | ||
| } | ||
| if (current.right != null) { | ||
| nodesInNextLevel.offer(current.right); | ||
| } | ||
| } | ||
| values.add(valuesInNextLevel); | ||
| nodes = nodesInNextLevel; | ||
| } | ||
| return values; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * LeetCodeの解法を参考にしたもの。 DFSでの解法。 BFSでなくともlevelで引数として渡せばOKという解法. stackつかってもできる。 | ||
| * 今回は、再帰の深さは最大で要素数nとなり、問題の制約は2000なのでまあ多分大丈夫かくらい | ||
| * | ||
| * <p>https://github.com/t0hsumi/leetcode/pull/26/files | ||
| * https://github.com/seal-azarashi/leetcode/pull/25/files でも大体同じ解法 | ||
| */ | ||
| public class solution2_1 { | ||
| public List<List<Integer>> levelOrder(TreeNode root) { | ||
| List<List<Integer>> values = | ||
| new ArrayList<>(); // https://github.com/t0hsumi/leetcode/pull/26/filesとかだとlevel_ordered_valuesにしてもう少し具体的にしてる。 | ||
| levelOrderHelper(root, 0, values); | ||
| return values; | ||
| } | ||
|
|
||
| private void levelOrderHelper(TreeNode node, int level, List<List<Integer>> values) { | ||
| if (node == null) { | ||
| return; | ||
| } | ||
| if (values.size() == level) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 上からしか呼ばないので、大きさが飛ばされることはないので、これでもいいですが、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます!たしかにwhileだとより明示的ですね |
||
| values.add(new ArrayList<>()); | ||
| } | ||
| List<Integer> list = values.get(level); | ||
| list.add(node.val); | ||
|
Comment on lines
+23
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 深さごとの部屋に追加していく操作は、こちらの解法でもありました。参考までに共有です。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. javaのListが参照渡しなのでこのような書き方をしても動くと思うのですが、ぱっと見 |
||
| levelOrderHelper(node.left, level + 1, values); | ||
| levelOrderHelper(node.right, level + 1, values); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** いただいたコメントを元にした解法 再帰部分での条件式をより読み手に伝わり例外なくListがある状態になることが一目でわかるようにwhileを使用。(読み手への配慮) */ | ||
| public class solution3_1 { | ||
| public List<List<Integer>> levelOrder(TreeNode root) { | ||
| List<List<Integer>> values = new ArrayList<>(); | ||
| levelOrderHelper(root, 0, values); | ||
| return values; | ||
| } | ||
|
|
||
| private void levelOrderHelper(TreeNode node, int level, List<List<Integer>> values) { | ||
| if (node == null) { | ||
| return; | ||
| } | ||
| while (values.size() <= level) { | ||
| values.add(new ArrayList<>()); | ||
| } | ||
| values.get(level).add(node.val); | ||
| levelOrderHelper(node.left, level + 1, values); | ||
| levelOrderHelper(node.right, level + 1, values); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
頭から尻尾まで舐めることしかしていないので、ArrayList でもいいかなと思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!