Wednesday, October 19, 2016

Find Leaves of Binary Tree

Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty.
Example:
Given binary tree 
          1
         / \
        2   3
       / \     
      4   5    
Returns [4, 5, 3], [2], [1].
Explanation:
1. Remove the leaves [4, 5, 3] from the tree
          1
         / 
        2          
2. Remove the leaf [2] from the tree
          1          
3. Remove the leaf [1] from the tree
          []         
Returns [4, 5, 3], [2], [1].

The most straightforward way is that, for each level, find leaves and remove those leaves, until we reach root. However, the complexity of this approach is extremely high. A better way is to track the depth of each level. If a node has no child, its a leave, and we assign its depth to 0. Now for each inner node, we first recursively find the max depth between its left and right child, then we calculate its depth by adding the maximum by 1. After we have the depth, we know where to add the node in the list.


public List<list<Integer>> findLeaves (TreeNode root) {
        List<list<Integer>> rst = new ArrayList<>();
        findLeaves(root, rst);
        return rst;
    }

    private int findLeaves(TreeNode root, List<list<Integer>> rst) {
        if (root == null) {
            return -1;
        }
        int depth = 1 + Math.max(findLeaves(root.left, rst), findLeaves(root.right, rst));
        if (depth >= rst.size()) {
            rst.add(new ArrayList<>());
        }
        rst.get(depth).add(root.val);
        return depth;
    }


1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete