일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- LeetCode #릿코드 #좋은 하루 되세요 #Have a nice day
- 아자아자 화이팅
- 네트워크
- I am Korean
- BFS
- Have a good day :)
- have a nice day
- Have a nice day.
- SSAFY 테스트
- 텐션 업 10기 화이팅
- DFS
- SSAFY IM/A
- SSAFY 10기 화이팅
- DP
- 자고 싶다
- 텐션 업 10기!
- Java 환경 설정
- Hamming weight
- 자료구조
- 우유가 옆으로 넘어지면 아야
- 우유아야
- 수학
- 코로나 싫어요
- 우유가옆으로넘어지면아야
- amazon
- 모르고리즘
- SeongSeobDang
- SSAFY 화이팅
- HAVE A GOOD DAY
- Today
- Total
Hope Everyone Is Happy
[Medium] 366. Find Leaves of Binary Tree (Java) 본문
[Medium] 366. Find Leaves of Binary Tree (Java)
J 크 2023. 12. 4. 15:16https://leetcode.com/problems/find-leaves-of-binary-tree/description/
Find Leaves of Binary Tree - LeetCode
Can you solve this real interview question? Find Leaves of Binary Tree - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
이걸 왜 오래푼거지,,
※ Question Summary
▶ Given the root of a binary tree, collect a tree's nodes under the following conditions
▶ Collect all the leafnodes
▶ Remove all the leaf nodes
▶ Repeat until the tree is empty
▶ Input : Treenode object
▶ Output : Leaf nodes stored in a 2D Integer ArrayList
◈ Constraints
- The number of nodes in the tree is in the range [1, 100].
- -100 <= Node.val <= 100
◈ Input - 1
[1,2,3,4,5]
◈ Output - 1
[[4,5,3],[2],[1]]
◈ Input - 2
[1]
◈ Output - 2
[[1]]
◎ HOW TO SOLVE IT
▶ When I find the leaf Node, store it in 1d ArrayList and delete it
▶ After finding all the leaf nodes, store the 1d ArrayList in a 2d ArrayList and delete it
▶ Repeat these two steps, until it's empty
▶ Create a method with params 'parentNode', 'childNode', and a boolean var for checking left or right
▶ If both the left and right nodes are null select as a leaf node
▶ If both the left and right nodes are not null, keep searching like dfs
/**
* 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 {
static List<Integer> saveNodes;
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
int nCount = 0;
while(root.left != null || root.right != null) {
saveNodes = new ArrayList<>();
searchTree(root, root.left, true);
searchTree(root, root.right, false);
result.add(saveNodes);
}
// add root node
saveNodes = new ArrayList<>();
saveNodes.add(root.val);
result.add(saveNodes);
return result;
}
// it will add val of leaf node and then delete it.
static public void searchTree(TreeNode parent, TreeNode temp, boolean isLeft) {
if(temp == null)
return;
if(temp.left == null && temp.right == null) {
saveNodes.add(temp.val);
if(isLeft)
parent.left = null;
else
parent.right = null;
return;
}
if(temp.left != null)
searchTree(temp, temp.left, true);
if(temp.right != null)
searchTree(temp, temp.right, false);
}
}
I HOPE YOUR DAY GOES WELL :)
'※ 릿코드 ( LeetCode ) > [Java] 문제 풀이 ( Solve the problems)' 카테고리의 다른 글
[Hard] 489. Robot Room Cleaner (Java) (0) | 2023.12.11 |
---|---|
[Medium] 419. Battleships in a Board (Java) (3) | 2023.12.05 |
[Easy] 191. Number of 1Bits (Java) (0) | 2023.11.29 |
[Hard] 329. Longest Increasing Path in a Matrix (Java) (0) | 2023.11.28 |
[Medium] 253. Meeting Rooms II (Java) (0) | 2023.11.27 |