Problem Solving with Algorithms

반응형

leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/

 

 

Medium

1013272Add to ListShare

Given the root of a binary tree, the depth of each node is the shortest distance to the root.

Return the smallest subtree such that it contains all the deepest nodes in the original tree.

A node is called the deepest if it has the largest depth possible among any node in the entire tree.

The subtree of a node is tree consisting of that node, plus the set of all descendants of that node.

Note: This question is the same as 1123: https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/

 

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4] Output: [2,7,4] Explanation: We return the node with value 2, colored in yellow in the diagram. The nodes coloured in blue are the deepest nodes of the tree. Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.

Example 2:

Input: root = [1] Output: [1] Explanation: The root is the deepest node in the tree.

Example 3:

Input: root = [0,1,3,null,2] Output: [2] Explanation: The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.

 

Constraints:

  • The number of nodes in the tree will be in the range [1, 500].
  • 0 <= Node.val <= 500
  • The values of the nodes in the tree are unique.

 

 

 

Solution


Approach 1: Paint Deepest Nodes

Intuition

We try a straightforward approach that has two phases.

The first phase is to identify the nodes of the tree that are deepest. To do this, we have to annotate the depth of each node. We can do this with a depth first search.

Afterwards, we will use that annotation to help us find the answer:

  • If the node in question has maximum depth, it is the answer.

  • If both the left and right child of a node have a deepest descendant, then the answer is this parent node.

  • Otherwise, if some child has a deepest descendant, then the answer is that child.

  • Otherwise, the answer for this subtree doesn't exist.

Algorithm

In the first phase, we use a depth first search dfs to annotate our nodes.

In the second phase, we also use a depth first search answer(node), returning the answer for the subtree at that node, and using the rules above to build our answer from the answers of the children of node.

Note that in this approach, the answer function returns answers that have the deepest nodes of the entire tree, not just the subtree being considered.

 

class Solution {
    Map<TreeNode, Integer> depth;
    int max_depth;
    public TreeNode subtreeWithAllDeepest(TreeNode root) {
        depth = new HashMap();
        depth.put(null, -1);
        dfs(root, null);
        max_depth = -1;
        for (Integer d: depth.values())
            max_depth = Math.max(max_depth, d);

        return answer(root);
    }

    public void dfs(TreeNode node, TreeNode parent) {
        if (node != null) {
            depth.put(node, depth.get(parent) + 1);
            dfs(node.left, node);
            dfs(node.right, node);
        }
    }

    public TreeNode answer(TreeNode node) {
        if (node == null || depth.get(node) == max_depth)
            return node;
        TreeNode L = answer(node.left),
                 R = answer(node.right);
        if (L != null && R != null) return node;
        if (L != null) return L;
        if (R != null) return R;
        return null;
    }
}

 

 

 

Complexity Analysis

  • Time Complexity: O(N), where N is the number of nodes in the tree.

  • Space Complexity: O(N).


Approach 2: Recursion

Intuition

We can combine both depth first searches in Approach #1 into an approach that does both steps in one pass. We will have some function dfs(node) that returns both the answer for this subtree, and the distance from node to the deepest nodes in this subtree.

Algorithm

The Result (on some subtree) returned by our (depth-first search) recursion will have two parts:

  • Result.node: the largest depth node that is equal to or an ancestor of all the deepest nodes of this subtree.
  • Result.dist: the number of nodes in the path from the root of this subtree, to the deepest node in this subtree.

We can calculate these answers disjointly for dfs(node):

  • To calculate the Result.node of our answer:

    • If one childResult has deeper nodes, then childResult.node will be the answer.

    • If they both have the same depth nodes, then node will be the answer.

  • The Result.dist of our answer is always 1 more than the largest childResult.dist we have.

 

class Solution {
    public TreeNode subtreeWithAllDeepest(TreeNode root) {
        return dfs(root).node;
    }

    // Return the result of the subtree at this node.
    public Result dfs(TreeNode node) {
        if (node == null) return new Result(null, 0);
        Result L = dfs(node.left),
               R = dfs(node.right);
        if (L.dist > R.dist) return new Result(L.node, L.dist + 1);
        if (L.dist < R.dist) return new Result(R.node, R.dist + 1);
        return new Result(node, L.dist + 1);
    }
}

/**
 * The result of a subtree is:
 *       Result.node: the largest depth node that is equal to or
 *                    an ancestor of all the deepest nodes of this subtree.
 *       Result.dist: the number of nodes in the path from the root
 *                    of this subtree, to the deepest node in this subtree.
 */
class Result {
    TreeNode node;
    int dist;
    Result(TreeNode n, int d) {
        node = n;
        dist = d;
    }
}

 

 

Complexity Analysis

  • Time Complexity: O(N), where N is the number of nodes in the tree.

  • Space Complexity: O(N).

 

 

 

 

result

class Solution {
    TreeNode ans;
    int maxDepth = 0;
    int minHeight = Integer.MAX_VALUE;
    public TreeNode subtreeWithAllDeepest(TreeNode root) {
        if (root == null) return null;
        getDepth(root, 0);
        return ans;
    }
    private int getDepth(TreeNode root, int height) {
        if (root == null) return 0;
        int left = getDepth(root.left, height + 1);
        int right = getDepth(root.right, height + 1);
        if (left == right) {
            if (height + left > maxDepth) {
                maxDepth = height + left;
                minHeight = height;
                ans = root;
            } else if (height + left == maxDepth) {
                if (height < minHeight) {
                    ans = root;
                    minHeight = height;
                }
            }
        }
        return Math.max(left , right) + 1;
    }
}
반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band