其他分享
首页 > 其他分享> > LeetCode题解之Leaf-Similar Trees

LeetCode题解之Leaf-Similar Trees

作者:互联网

1、题目描述

2、问题分析

将叶子节点的值放入vector,然后比较。

 

3、代码

 1 bool leafSimilar(TreeNode* root1, TreeNode* root2) {
 2         vector<int> v1;
 3         vector<int> v2;
 4         
 5         findLeaf(root1, v1);
 6         findLeaf(root2, v2);
 7         
 8         return v1 == v2;
 9         
10     }
11     
12     void findLeaf(TreeNode *root, vector<int> &v)
13     {
14         if (root == NULL)
15             return;
16         if (root->left == NULL && root->right == NULL){
17             v.push_back(root->val);
18             return ;
19         }
20         findLeaf(root->left,v);
21         findLeaf(root->right,v);
22     }

 

标签:Leaf,TreeNode,题解,return,vector,root,NULL,Similar,findLeaf
来源: https://www.cnblogs.com/wangxiaoyong/p/10426149.html