码迷,mamicode.com
首页 > 其他好文 > 详细

650. Find Leaves of Binary Tree

时间:2019-08-09 01:39:10      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:div   rem   val   null   leaves   while   tree   code   vector   

 

 

class Solution {
public:
    vector<vector<int>> findLeaves(TreeNode* root) {
        vector<vector<int>> res;
        while (root) {
            vector<int> leaves;
            root = remove(root, leaves);
            res.push_back(leaves);
        }
        return res;
    }
    TreeNode* remove(TreeNode* node, vector<int>& leaves) {
        if (!node) return NULL;
        if (!node->left && !node->right) {
            leaves.push_back(node->val);
            return NULL;
        }
        node->left = remove(node->left, leaves);
        node->right = remove(node->right, leaves);
        return node;
    }
};

 

650. Find Leaves of Binary Tree

标签:div   rem   val   null   leaves   while   tree   code   vector   

原文地址:https://www.cnblogs.com/ymjyqsx/p/11324692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!