1. 程式人生 > >865. Smallest Subtree with all the Deepest Nodes(python+cpp)

865. Smallest Subtree with all the Deepest Nodes(python+cpp)

題目:

Given a binary tree rooted at root, the depth of each node is the shortest distance to the root.
A node is deepest if it has the largest depth possible among any node in the entire tree.
The subtree of a node is that node, plus the set of all descendants of that node.
Return the node with the largest depth such that it contains all the deepest nodes in its subtree.
Example 1:

Input: [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 colored in blue are the deepest nodes of the tree. The input
“[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]” is a serialization of the given tree. The output “[2, 7,
4]” is a serialization of the subtree rooted at the node with value 2. Both the input and
output have TreeNode type.
Note:


The number of nodes in the tree will be between 1 and 500.
The values of each node are unique.

解釋:
用dfs做。
python程式碼:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution
(object): def subtreeWithAllDeepest(self, root): """ :type root: TreeNode :rtype: TreeNode """ def depth(root): left,right=0,0 if not root.left and not root.right: return root,1 if root.left and root.right: tmp_left,left=depth(root.left) tmp_right,right=depth(root.right) root_depth=max(left,right)+1 if left==right: return root,root_depth elif left>right: return tmp_left,root_depth else: return tmp_right,root_depth if root.left: tmp_left,left=depth(root.left) return tmp_left,left+1 if root.right: tmp_right,right=depth(root.right) return tmp_right,right+1 if not root: return None return depth(root)[0]

c++程式碼:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* subtreeWithAllDeepest(TreeNode* root) {
        if (!root)
            return NULL;
        int d=0;
        return depth(root,d);      
    }
    TreeNode* depth(TreeNode* root,int &d)
    {
        if (!root->left && !root->right)
        {
            d=1;
            return root;
        }
        int left_depth=0,right_depth=0;
        if (root->left && root->right)
        {
            TreeNode* tmp_left=depth(root->left,left_depth);
            TreeNode* tmp_right=depth(root->right,right_depth);
            if (left_depth==right_depth)
            {
                d=left_depth+1;
                return root;
            }
            else if(left_depth>right_depth)
            {
                d=left_depth+1;
                return tmp_left;
            }
            else
            {
                d=right_depth+1;
                return tmp_right;
            }
        }
        if(root->left)
        {
            TreeNode* tmp_left=depth(root->left,left_depth);
            d=left_depth+1;
            return tmp_left;
        }
        if(root->right)
        {
            TreeNode* tmp_right=depth(root->right,right_depth);
            d=right_depth+1;
            return tmp_right;
        }
    }
};

總結:
剛開始寫得時候先dfs求深度再做外層的dfs,後來發現不需要這麼耗時,直接讓depth()函式返回兩個引數就好。
但是c++如何返回兩個引數?深度用引用表示吧。
在論壇上問人,原來c++也有比較方便的split()函式,是boost::split,使用起來非常方便,激動!!!