Hope Everyone Is Happy

[Medium] 366. Find Leaves of Binary Tree (Java) 본문

※ 릿코드 ( LeetCode )/[Java] 문제 풀이 ( Solve the problems)

[Medium] 366. Find Leaves of Binary Tree (Java)

J 크 2023. 12. 4. 15:16
728x90
반응형

https://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 :)