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 ...
随机推荐
- 识别低效率的SQL语句
1.返回行与逻辑读的比率 CREATE TABLE t as select * from dba_objects; --CREATE INDEX idx ON t (object_id); ---例1 ...
- MVC HtmlHelper
HTML扩展类的所有方法都有2个参数: 以textbox为例子 public static string TextBox( this HtmlHelper htmlHelper, string nam ...
- java面向对象编程——第二章 java基础语法
第二章 java基础语法 1. java关键字 abstract boolean break byte case catch char class const continue default do ...
- Phonegap hello world 不容易啊~!
今天一个项目要用phonegap,当初就是觉得phonegap配置太tmd的麻烦了,所以转头appcan,但今天项目必须用-- 先是看到官方说用nodejs装,tmd的,总是重复同一个错误,安装不起, ...
- 服务器端与客户端TCP连接入门(一)
Java中使用Socket(即套接字)完成TCP程序的开发 服务器端使用ServerSocket接收客户端的连接请求,每一个客户端都使用一个Socket对象表示 在服务器端每次运行时都要使用accep ...
- 精华 ionic入门之色彩、图标、边距和界面组件:列表
目录:色彩.图标和边距色彩图标内边距界面组件:列表列表:.list成员容器:.item.item: 嵌入文本.item : 嵌入图标.item : 嵌入头像.item : 嵌入缩略图.item : 嵌 ...
- 线程系列4---sleep()和wait()方法区别
2013-12-25 14:49:00 1. sleep()方法是Thread类的一个静态方法,可以在任意地方被调用,而wait()方法是object类的一个方法,只能在同步代码块或者同步方法里面,通 ...
- android自定义控件实例(Linearlayout组合TextView和ImageView)
2013-12-18 11:25:22 转载自: http://www.open-open.com/lib/view/open1328836804515.html 很多时候android常用的控件不能 ...
- wp8.1 C#技巧: Data和ViewModel类编写
在Data.cs namespace PicApp { [DataContract] class DataItem : PropertyChangeNotification { public even ...
- auto_ptr的使用原则
auto_ptr是c++标准库中的一种严格所有权型的智能指针,实现在backward/auto_ptr.h文件中 pro: 1.做临时变量时,不需要手动去释放资源 void f() { ClassA ...