📜  具有所有最深节点的最小子树

📅  最后修改于: 2021-05-04 15:00:04             🧑  作者: Mango

给定一个二叉树,任务是找到包含给定二叉树的所有节点最深最小子树,并返回子树的根。
注意:每个节点的深度定义为从根到给定节点的路径的长度。

例子:

方法:请按照以下步骤解决问题:

  • 使用DFS递归遍历二叉树。
  • 对于每个节点,找到其左侧和右侧子树的深度。
  • 如果左侧子树的深度>右侧子树的深度:遍历左侧子树。
  • 如果右侧子树的深度>左侧子树的深度:遍历右侧子树。
  • 否则,返回当前节点。

下面是上述方法的实现:

C++
1
          /   \
         2     3
       /  \   /  \
      4    5  6   7
                 /  \
                8    9


Java
1
             /   \
            2     3


Python3
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Structure of a Node
struct TreeNode {
 
    int val;
    TreeNode* left;
    TreeNode* right;
 
    TreeNode(int data)
    {
        this->val = data;
        left = NULL;
        right = NULL;
    }
};
 
// Function to return depth of
// the Tree from root
int find_ht(TreeNode* root)
{
    if (!root)
        return 0;
 
    // If current node is a leaf node
    if (root->left == NULL
        && root->right == NULL)
        return 1;
 
    return max(find_ht(root->left),
               find_ht(root->right))
           + 1;
}
 
// Function to find the root of the smallest
// subtree consisting of all deepest nodes
void find_node(TreeNode* root, TreeNode*& req_node)
{
    if (!root)
        return;
 
    // Stores height of left subtree
    int left_ht = find_ht(root->left);
 
    // Stores height of right subtree
    int right_ht = find_ht(root->right);
 
    // If height of left subtree exceeds
    // that of the right subtree
    if (left_ht > right_ht) {
 
        // Traverse left subtree
        find_node(root->left, req_node);
    }
 
    // If height of right subtree exceeds
    // that of the left subtree
    else if (right_ht > left_ht) {
        find_node(root->right, req_node);
    }
 
    // Otherwise
    else {
 
        // Return current node
        req_node = root;
        return;
    }
}
 
// Driver Code
int main()
{
    struct TreeNode* root
        = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
 
    TreeNode* req_node = NULL;
 
    find_node(root, req_node);
 
    cout << req_node->val;
 
    return 0;
}


C#
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
// Structure of a Node
static class TreeNode
{
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int data)
    {
        this.val = data;
        left = null;
        right = null;
    }
};
 
// Function to return depth of
// the Tree from root
static int find_ht(TreeNode root)
{
    if (root == null)
        return 0;
 
    // If current node is a leaf node
    if (root.left == null
        && root.right == null)
        return 1;
    return Math.max(find_ht(root.left),
               find_ht(root.right))
           + 1;
}
 
// Function to find the root of the smallest
// subtree consisting of all deepest nodes
static TreeNode find_node(TreeNode root, TreeNode req_node)
{
    if (root == null)
        return req_node;
 
    // Stores height of left subtree
    int left_ht = find_ht(root.left);
 
    // Stores height of right subtree
    int right_ht = find_ht(root.right);
 
    // If height of left subtree exceeds
    // that of the right subtree
    if (left_ht > right_ht)
    {
 
        // Traverse left subtree
        req_node = find_node(root.left, req_node);
    }
 
    // If height of right subtree exceeds
    // that of the left subtree
    else if (right_ht > left_ht)
    {
        req_node = find_node(root.right, req_node);
    }
 
    // Otherwise
    else
    {
 
        // Return current node
        req_node = root;
        return req_node;
    }
    return req_node;
}
 
// Driver Code
public static void main(String[] args)
{
    TreeNode root = new TreeNode(1);
    root.left = new TreeNode(2);
    root.right = new TreeNode(3);
    TreeNode req_node = null;
    req_node = find_node(root, req_node);
    System.out.print(req_node.val);
}
}
 
// This code is contributed by 29AjayKumar


输出:
# Python3 program to implement
# the above approach
 
# Structure of a Node
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
 
# Function to return depth of
# the Tree from root
def find_ht(root):
    if (not root):
        return 0
 
    # If current node is a leaf node
    if (root.left == None and root.right == None):
        return 1
 
    return max(find_ht(root.left), find_ht(root.right)) + 1
 
# Function to find the root of the smallest
# subtree consisting of all deepest nodes
def find_node(root):
    global req_node
 
    if (not root):
        return
 
    # Stores height of left subtree
    left_ht = find_ht(root.left)
 
    # Stores height of right subtree
    right_ht = find_ht(root.right)
 
    # If height of left subtree exceeds
    # that of the right subtree
    if (left_ht > right_ht):
 
        # Traverse left subtree
        find_node(root.left)
 
    # If height of right subtree exceeds
    # that of the left subtree
    elif (right_ht > left_ht):
        find_node(root.right)
 
    # Otherwise
    else:
 
        # Return current node
        req_node = root
        return
 
# Driver Code
if __name__ == '__main__':
 
    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    req_node = None
    find_node(root)
    print(req_node.val)
 
    # This code is contributed by mohit kumar 29

时间复杂度: O(NlogN)
最坏情况下的复杂性发生在倾斜的二叉树上,其遍历左右子树需要O(N)复杂度,计算子树的高度需要O(logN)计算复杂度。
辅助空间: O(1)