일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- vue.js
- webpack
- git
- 앙상블
- leetcode
- scss
- 보안
- machine learning
- C++
- TypeScript
- Python
- var
- bash
- security
- vuetify
- AI
- generic
- C#
- dotenv
- JavaScript
- property
- docker
- BOJ
- Clone
- loop
- npm
- type
- nginx
- condition
- VUE
Archives
- Today
- Total
ice rabbit programming
[LeetCode] Invert Binary Tree 본문
https://leetcode.com/problems/invert-binary-tree/
이전 문제에서 List를 뒤집었다면 이번에는 Tree를 뒤집는 문제이다. 재귀를 이용해서 left와 right를 뒤집는 로직이다.
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode InvertTree(TreeNode root) {
if(root==null)
return null;
TreeNode result=new TreeNode(root.val);
if(root.left!=null)
result.right=InvertTree(root.left);
if(root.right!=null)
result.left=InvertTree(root.right);
return result;
}
}
'PS > LeetCode' 카테고리의 다른 글
[LeetCode] Employees Earning More Than Their Manager (0) | 2020.05.01 |
---|---|
[LeetCode] Symmetric Tree (0) | 2020.05.01 |
[LeetCode] Reverse Linked List (0) | 2020.04.27 |
[LeetCode] Combine Two Tables (0) | 2020.04.27 |
[LeetCode] Customers Who Never Order (0) | 2020.04.26 |