美文网首页
872.leaf-similar-trees

872.leaf-similar-trees

作者: Optimization | 来源:发表于2020-05-25 17:30 被阅读0次
    class Solution {
    public:
        bool leafSimilar(TreeNode* root1, TreeNode* root2) {
            if(!root1 && !root2) return true;
            vector<int> vecLeaf1;
            vector<int> vecLeaf2;
            getLeafNode(root1, vecLeaf1);
            getLeafNode(root2, vecLeaf2);
            return (vecLeaf1 == vecLeaf2);
        }
    private:
        void getLeafNode(TreeNode* root, vector<int>& leafVal){
            if(!root) return;
            if(!root->left&&!root->right){
                leafVal.push_back(root->val);
            }
            getLeafNode(root->left, leafVal);
            getLeafNode(root->right, leafVal);
        }
    };
    

    相关文章

      网友评论

          本文标题:872.leaf-similar-trees

          本文链接:https://www.haomeiwen.com/subject/iyxuahtx.html