Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions invert-binary-tree/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time complexity: O(n)
// Space complexity: O(n)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function (root) {
const dfs = (current) => {
if (!current) {
return;
}

const temp = current.left;
current.left = current.right;
current.right = temp;

dfs(current.left);
dfs(current.right);
};

dfs(root);

return root;
};
Comment on lines +12 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DFS와 재귀를 활용하면 더 깔끔한 코드로 구현할 수 있겠어요. 참고가 되었습니다. 감사합니다!