-
-
Notifications
You must be signed in to change notification settings - Fork 245
[forest000014] Week 10 #1016
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
Merged
Merged
[forest000014] Week 10 #1016
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
# Time Complexity: O(n) | ||
- 전체 노드를 1번씩 탐색 | ||
# Space Complexity: O(n) | ||
- 재귀 호출의 각 depth마다 temp 노드 하나씩 생성 | ||
# Solution | ||
- 현재 노드의 왼쪽 자식과 오른쪽 자식을 각각 재귀 호출하여 자식의 자식 노드들을 반전시킨뒤, | ||
- 왼쪽 자식과 오른쪽 자식을 반전시킵니다. | ||
- base condition으로, 현재 노드가 null인 경우 null을 early return 합니다. | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
public TreeNode invertTree(TreeNode root) { | ||
if (root == null) { | ||
return null; | ||
} | ||
|
||
invertTree(root.left); | ||
invertTree(root.right); | ||
|
||
TreeNode temp = root.left; | ||
root.left = root.right; | ||
root.right = temp; | ||
|
||
return root; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
요 부분은 저도 피드백 받았는데 TreeNode의 생성자가 오버로딩 되있기에 이를 활용하면 내부 로직을 엄청 간소화할 수 있더라구요(메모리도 아낄 수 있고). 한번 검토해보시는걸 추천합니다!