LeetCode 257.二叉树所有路径(C++)
给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
输入:
1
/ \
2 3
\
5
输出: ["1->2->5", "1->3"]
解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
if(root == NULL)//二叉树为空
return vec;
DFS(root, to_string(root->val));
return vec;
}
void DFS(TreeNode* root, string str) {
if (root->right == NULL && root->left == NULL) {//搜索完一个叶子节点,将数据存入容器
vec.push_back(str);
return;
}
if(root->left != NULL)//防止越界取值
DFS(root->left, str + "->" + to_string(root->left->val));//先将左子叶遍历,使用str存储递归中经过的值
if(root->right != NULL)
DFS(root->right, str + "->" + to_string(root->right->val));
}
private:
vector<string> vec;
};
转载:这个会更好理解
/*这个应该挺容易理解*/
class Solution {
private:
vector<string> ans;// 最终的解答
public:
vector<string> binaryTreePaths(TreeNode* root) {
binaryTreePaths(root, "", true);// 递归求解
return ans;
}
private:
void binaryTreePaths(TreeNode* root, string s, bool isRoot) {
if (!root) return;
s += (isRoot ? "" : "->") + to_string(root->val);//根节点需要特殊处理
if (!root->left && !root->right) {// 如果找到一个叶子节点
ans.push_back(s);
return;
}
binaryTreePaths(root->left, s, false);
binaryTreePaths(root->right, s, false);
}
};
LeetCode 257.二叉树所有路径(C++)的更多相关文章
- Java实现 LeetCode 257 二叉树的所有路径
257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ...
- LeetCode 257 二叉树的所有路径
题目: 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5&quo ...
- leetcode 257. 二叉树的所有路径 包含(二叉树的先序遍历、中序遍历、后序遍历)
给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \2 3 \ 5 输出: ["1->2->5", & ...
- 【Leetcode】二叉树简单路径最大和问题
问题一:二叉树任意两个叶子间简单路径最大和 示例: -100 / \ 2 100 / \ 10 20 思路:这个问题适用于递归思路. 首先,将问题简单化:假设包含最大和summax的简单 ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- Linux中的SELinux与chcon以及Samba实现【转】
一.SELinux SElinux的前身是NSA(美国国家安全局)发起的一个项目.它的目的是将系统加固到可以达到军方级别. 为什么NSA选择Linux呢? 在目前市面上大多数操作系统都是商用闭源的,只 ...
- Windows中与系统关联自己开发的程序(默认打开方式、图标、右击菜单等)
1. 默认打开方式 1.1. 代码支持 在Windows下,某个特定后缀名类型的文件,如果要双击时默认用某个程序(比如自己开发的WinForm程序)打开,代码中首先肯定要支持直接根据这个文件进行下一步 ...
- 为什么 kubernetes 天然适合微服务 (3)
此文已由作者刘超授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 四.Kubernetes 本身就是微服务架构 基于上面这十个设计要点,我们再回来看 Kubernetes,会发现 ...
- 201621123012 《java程序设计》第4周学习总结
1. 本章学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答:关键词:继承 多态 抽象类 abstract 覆盖 object siper 1.2 尝试使用思维导图将这些关键词组织起来. ...
- 可变大小、颜色边框、样式的UISwitch
1.CHSwitch.h // // 文 件 名:CHSwitch.h // // 版权所有:Copyright © 2018 lelight. All rights reserved. // 创 建 ...
- ES更改参数max_result_window
今天开发那边说翻页超过10000报错.早上来查阅官网手册,说from/size默认是10000.通过参数index.max_result_window进行控制.那么直接改这个参数即可. 1.先看看默认 ...
- 搭建jumpserver堡垒机
环境 系统: CentOS 7 IP: 192.168.244.144 关闭 selinux 和防火墙 # CentOS 7 $ setenforce 0 # 可以设置配置文件永久关闭 $ syst ...
- webstorm激活服务器地址
2017.1.4版本可用 http://idea.imsxm.com/
- explian执行计划
MySQL为我们提供了 explain 关键字来直观的查看一条SQL的执行计划. explain显示了MySQL如何使用索引来处理select语句以及连接表,可以帮助选择更好的索引和写出更优化的查询语 ...
- Android Studio 常用应用
1.在控制台的Logcat中输出测试语句 package com.example.lucky.helloworld; import android.support.v7.app.AppCompatAc ...