LeetCode Binary Tree Paths(简单题)
题意:
给出一个二叉树,输出根到所有叶子节点的路径。
思路:
直接DFS一次,只需要判断是否到达了叶子,是就收集答案。
/**
* 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 {
vector<string> ans;
public:
void DFS(string path,TreeNode* t)
{
if(t->left==NULL&&t->right==NULL)
{
ans.push_back(path);
return ;
} if(t->left)
DFS(path+"->"+to_string(t->left->val),t->left);
if(t->right)
DFS(path+"->"+to_string(t->right->val),t->right);
}
vector<string> binaryTreePaths(TreeNode* root) {
if(root!=NULL) DFS(to_string(root->val),root);
return ans;
}
};
AC代码
LeetCode Binary Tree Paths(简单题)的更多相关文章
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode——Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- Python3解leetcode Binary Tree Paths
问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...
- LEETCODE —— Binary Tree的3 题 —— 3种非Recursive遍历
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- leetcode Binary Tree Paths python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
随机推荐
- 通过class和id获取DOM元素的区别
1.通过id获取DOM元素的方法:document.getElementById("id名") 2.通过class获取DOM元素的方法:document.getElementsBy ...
- c++ boost 汉字和模式串混用的例子
*=============================================================== * Copyright (C) All rights reserved ...
- WebAPI用法
ASP.NET Web API(一):使用初探,GET和POST数据[Parry] HttpClient + ASP.NET Web API, WCF之外的另一个选择[dudu] 通过这两篇文章让我了 ...
- formvalidator4.1.3 使用过程中一些问题的解决
在使用formvalidator4.1.3 插件时 发现 正常情况调用时没有问题的,但是我们的项目中需要把css .js 之类的静态资源用单独的域名加载. 那么问题就来了在 该插件中 有一段这样的 ...
- 对于服务器的识别的条件,header之类的使用
根据上一节的内容的衔接 一:urllib.request的使用 headers的一些属性 User-Agent : 有些服务器或 Proxy 会通过该值来判断是否是浏览器发出的请求Content-Ty ...
- 主机无法访问虚拟机上的elasticsearch服务器
问题: es在linux上搭建好,通过curl -XGET ip:port可以获得服务器信息展示,但是主机在浏览器上无法访问. 原因: 主机通过telnet访问linux的80端口,发现是不通的.可以 ...
- 二模 (9) day2
第一题: 题目大意:求满足条件P的N位二进制数的个数.P:该二进制数有至少3个0或者3个1挨在一起.. N<=20000 解题过程: 1.一开始直接写了个dfs把表打了出来,不过没发现什么规律, ...
- Catalan数
先看2个问题: 问题一: n个元素进栈(栈无穷大),进栈顺序为1,2,3,....n,那么有多少种出栈顺序? 先从简单的入手:n=1,当然只有1种:n=2,可以是1,2 也可以是2,1:那么有2种: ...
- C# DES加密
需要引用名称空间 using System; using System.Text; using System.Security.Cryptography; using System.IO; 具体代码: ...
- MATLAB图像处理函数汇总(二)
60.imnoise 功能:增加图像的渲染效果. 语法: J = imnoise(I,type) J = imnoise(I,type,parameters) 举例 I = imread('eight ...