博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
872. Leaf-Similar Trees
阅读量:4614 次
发布时间:2019-06-09

本文共 1302 字,大约阅读时间需要 4 分钟。

 

按序记录每棵树叶子节点。比较是否一致。

前序,中序,后序都可以。

Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form a leaf value sequence.

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

 

Note:

  • Both of the given trees will have between 1 and 100 nodes.
/** * 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:    bool leafSimilar(TreeNode* root1, TreeNode* root2) {        vector
res1; vector
res2; preorder(root1,res1); preorder(root2,res2); if(res1.size()!=res2.size()) return false; for(int i=0;i
& res){ if(root==NULL) return; if(root->left==NULL && root->right==NULL){ res.push_back(root->val); return; } preorder(root->left,res); preorder(root->right,res); }};

 ac后看了solution, 

if(res1.size()!=res2.size())            return false;        for(int i=0;i

可以简写为

return res1==res2;

 

转载于:https://www.cnblogs.com/learning-c/p/9787656.html

你可能感兴趣的文章
Linux——静态库和动态库的生成
查看>>
Ulfius交叉编译——搭建http服务器端
查看>>
Linux——进度条实现
查看>>
Linux——定时器与计时器
查看>>
Linux——makefile编写
查看>>
vi/vim使用
查看>>
C/C++——指针
查看>>
讨论Spring整合Mybatis时一级缓存失效得问题
查看>>
Maven私服配置Setting和Pom文件
查看>>
Linux搭建Nexus3.X构建maven私服
查看>>
Notepad++使用NppFTP插件编辑linux上的文件
查看>>
NPOI 操作Excel
查看>>
MySql【Error笔记】
查看>>
vue入门
查看>>
JS线程Web worker
查看>>
Flex的动画效果与变换!(三)(完)
查看>>
mysql常见错误码
查看>>
Openresty 与 Tengine
查看>>
使用XV-11激光雷达做hector_slam
查看>>
布局技巧4:使用ViewStub
查看>>