leetcode257 Binary Tree Paths
来源:互联网 发布:薄膜厚度测量实验数据 编辑:程序博客网 时间:2024/06/07 01:54
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"]import java.util.ArrayList;import java.util.List;/** * 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"] * * */public class Solution {<span style="white-space:pre"></span>/*树的后序遍历代码*/public List<String> binaryTreePaths1(TreeNode root) {ArrayList<TreeNode> treeList = new ArrayList<TreeNode>();List<String> res = new ArrayList<String>();TreeNode p = null;int flag, i, top = -1;if (root != null) {do {while (root != null) {top++;treeList.add(root);root = root.left;}p = null;flag = 1;while (top != -1 && flag != 0) {root = treeList.get(top);if (root.right == p) {/* * if(root.left==null && root.right==null) { * System.out.println(root.val); } */System.out.println(root.val);treeList.remove(top);top--;p = root;} else {root = root.right;flag = 0;}}} while (top != -1);}return res;}<span style="white-space:pre"></span>/*可以AC的程序代码*/public List<String> binaryTreePaths(TreeNode root) {ArrayList<TreeNode> treeList = new ArrayList<TreeNode>();List<String> res = new ArrayList<String>();int flag, i, top = -1;TreeNode q = root;TreeNode p = null;do {while (q != null) {top++;treeList.add(q);q = q.left;}p = null;flag = 1;while (top != -1 && flag != 0) {q = treeList.get(top);if (q.right == p) {//System.out.println(q.val);if(q.left==null && q.right ==null){String re = "";for(int i1 = 0;i1<top;i1++){re+=(treeList.get(i1).val+"->");}re+=treeList.get(top).val;res.add(re);}treeList.remove(top);top--;p = q;} else {q = q.right;flag = 0;}}} while (top != -1);/*for(int i1 = 0;i1<res.size();i1++){System.out.println(res.get(i1));}*/return res;}public static void main(String[] args) {TreeNode root = new TreeNode(1);TreeNode l1 = new TreeNode(2);TreeNode l2 = new TreeNode(3);TreeNode l3 = new TreeNode(5);root.left = l1;root.right = l2;l1.right = l3;new Solution().binaryTreePaths(root);}}class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}}
0 0
- [LeetCode257] Binary Tree Paths
- leetcode257 Binary Tree Paths
- leetcode257---Binary Tree Paths
- LeetCode257:Binary Tree Paths
- LeetCode257. Binary Tree Paths
- leetcode257 Binary Tree Paths
- leetcode257. Binary Tree Paths
- LeetCode257——Binary Tree Paths
- LeetCode257. Binary Tree Paths(dfs)
- Binary Tree Paths
- Leetcode: Binary Tree Paths
- [LeetCode] Binary Tree Paths
- [Leetcode]Binary Tree Paths
- leetcode:Binary Tree Paths
- Binary Tree Paths
- [leetcode] Binary Tree Paths
- 257 Binary Tree Paths
- Binary Tree Paths
- easyui 框架的一些属性
- Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询(转)
- 网络编程基础1-字节序
- NullPointerException at android.widget.AbsListView.obtainView.
- 删除模糊表名的所有表,存储过程
- leetcode257 Binary Tree Paths
- 简单粗暴的eclipse hadoop 2.5.2 插件
- ALLJOYN® SYSTEM DESCRIPTION
- iOS 写SDK注意事项
- nonce和timestamp在Http安全协议中的作用
- 关于W制式下的状态介绍,CELL_PCH/CELL_FACH/CELL_DCH等
- RUNLOOP小结
- 有1000桶酒,其中1桶有毒。请问最少需要多少只老鼠
- android Titlebar一行代码实现沉浸式效果