leetcode 257. Binary Tree Paths 深度优先遍历DFS
来源:互联网 发布:pop3 默认端口 编辑:程序博客网 时间:2024/06/04 17:52
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
[“1->2->5”, “1->3”]
直接DFS深度优先遍历即可。
代码如下:
import java.util.ArrayList;import java.util.List;/*class TreeNode{ int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}*//* * 这道题其实很简单,但是要注意递归开始的起点和终点 * Java中使用StringBuilder会更快点 * */public class Solution { List<String> res=new ArrayList<String>(); public List<String> binaryTreePaths(TreeNode root) { if(root==null) return res; //getAllPath(root,root.val+""); List<Integer> one=new ArrayList<>(); one.add(root.val); getAllPath(root, one); return res; } private void getAllPath(TreeNode root, List<Integer> one) { if(root.left==null && root.right==null) { if(one.size()==1) res.add(one.get(0)+""); else { StringBuilder lastBuilder=new StringBuilder(); lastBuilder.append(one.get(0)); for(int i=1;i<one.size();i++) lastBuilder.append("->"+one.get(i)); res.add(lastBuilder.toString()); } } if(root.left!=null) { one.add(root.left.val); getAllPath(root.left, one); one.remove(one.size()-1); } if(root.right!=null) { one.add(root.right.val); getAllPath(root.right, one); one.remove(one.size()-1); } } private void getAllPath(TreeNode root, String path) { if(root.left==null && root.right==null) res.add(path); if(root.left!=null) getAllPath(root.left, path+"->"+root.left.val); if(root.right!=null) getAllPath(root.right, path+"->"+root.right.val); }}
下面是C++的做法,就是做一个简单的DFS深度优先遍历,代码如下:
#include <iostream>#include <vector>#include <string>using namespace std;/*struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};*/class Solution {public: vector<string> res; vector<string> binaryTreePaths(TreeNode* root) { if (root == NULL) return res; getAll(root, to_string(root->val)); return res; } void getAll(TreeNode* root, string one) { if (root->left == NULL && root->right == NULL) res.push_back(one); if (root->left != NULL) getAll(root->left, one + "->" + to_string(root->left->val)); if (root->right != NULL) getAll(root->right, one + "->" + to_string(root->right->val)); }};
阅读全文
0 0
- leetcode 257. Binary Tree Paths 深度优先遍历DFS
- leetcode 655. Print Binary Tree 深度优先遍历DFS
- leetcode 606. Construct String from Binary Tree 前序遍历 + 深度优先遍历DFS
- leetcode 257. Binary Tree Paths(DFS)
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- leetcode 111. Minimum Depth of Binary Tree DFS深度优先遍历 + 添加对叶子节点判断
- leetcode 543. Diameter of Binary Tree 最长树的片段 + 深度优先遍历DFS
- leetcode 671. Second Minimum Node In a Binary Tree 第二小数字 + 深度优先遍历DFS
- leetcode 669. Trim a Binary Search Tree 修建二叉搜索树BST + 深度优先遍历DFS
- leetcode 662. Maximum Width of Binary Tree 深度优先遍历DFS
- leetcode -- 257. Binary Tree Paths【遍历次序】
- leetcode 617. Merge Two Binary Trees 深度优先遍历DFS
- leetcode 100. Same Tree 二叉树DFS深度优先遍历
- leetcode 572. Subtree of Another Tree 深度优先遍历DFS
- 257.leetcode Binary Tree Paths(easy)[二叉树深度路径遍历]
- DFS:257. Binary Tree Paths
- leetcode_257. Binary Tree Paths 二叉树深度优先遍历,深拷贝的运用
- LeetCode Binary Tree Paths 树 + DFS
- 还是关于g2o初始化一些
- 排序(五):堆排序
- 将Android.mk转换成Cmake使用
- Android逆向反编译基本方法
- 单例设计模式
- leetcode 257. Binary Tree Paths 深度优先遍历DFS
- IAP是什么
- MySQL 5.7 sql_mode设置 分组查询报错
- 数据库连接池
- JavaScript的闭包详解
- maven使用命令创建项目失败[ERROR] Could not find goal 'create' in plugin org.apache.maven.plugins:maven-archety
- JavaScript(六)
- stm32f103系列芯片P15端口当做普通IO使用
- 【22】字体效果(阴影/浮雕)