zoukankan      html  css  js  c++  java
  • leetcode257 C++ 4ms 二叉树的所有路径

    /**
     * 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:
        void recur(TreeNode* root, vector<string>& res, string str){
            if(root->left == nullptr && root->right == nullptr){
                res.push_back(str);
                return;
            }
            
            if(root->left != nullptr){
                recur(root->left, res, str + "->" + to_string(root->left->val));
            }
    
            if(root->right != nullptr){
                recur(root->right, res, str + "->" + to_string(root->right->val));
            }
        }
    
        vector<string> binaryTreePaths(TreeNode* root) {
            vector<string> res;
            if(root == nullptr){
                return res;
            }
            recur(root, res, to_string(root->val));
            return res;
        }
    };
    
  • 相关阅读:
    React-使用combineReducers完成对数据对拆分管理
    Linux
    Linux
    linux
    linux
    Linux
    Linux
    Linux
    Linux
    Linux 系统基础优化和常用命令
  • 原文地址:https://www.cnblogs.com/theodoric008/p/9402846.html
Copyright © 2011-2022 走看看