LeetCode@DFS_257_Binary_Tree_Paths
来源:互联网 发布:绘图纸用什么软件 编辑:程序博客网 时间:2024/06/07 18:06
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"]
java:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {public List<String> binaryTreePaths(TreeNode root){List<String> path = new LinkedList<>();Stack<TreeNode> node = new Stack<>();Stack<String> subPath = new Stack<>();if(root == null){return path;}node.push(root);subPath.push("");while (!node.isEmpty()){TreeNode currNode = node.pop();String curr = subPath.pop();//stack.pop();if (currNode.left != null){subPath.push(curr+currNode.val+"->");node.push(currNode.left);}if (currNode.right != null){subPath.push(curr+currNode.val+"->");node.push(currNode.right);}if (currNode.left == null && currNode.right == null){path.add(curr+currNode.val);}}return path;} }
阅读全文
0 0
- LeetCode@DFS_257_Binary_Tree_Paths
- LeetCode@DFS_257_Binary_Tree_Paths
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- ### laravel学习--控制器
- Virtual Box(虚拟机)笔记
- mysql单表统计认证时间间隔小于15天的人数和次数
- 生成对抗网络学习笔记2----GANs(Generative Adversarial Nets)总结
- var_export、json_encode截断字符串
- LeetCode@DFS_257_Binary_Tree_Paths
- linux安装两个apache
- 备份CSDN博客——dump到本地存档/博客搬家
- day10 指针、字符串字面值、字符数组
- 构造——洛谷 P1416 攻击火星
- 根据子网掩码计算最大主机数
- C++:STL标准入门汇总
- Failed to check the status of the service com.taotao.search.service.SearchItemSerivce. No provider a
- 5_paramiko_SSHException Invalid requirement, parse error at