利用树的 先序遍历,采用递归和迭代方式实现
迭代方式有待优化
/*
* @lc app=leetcode.cn id=257 lang=java
*
* [257] 二叉树的所有路径
*/
// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
/**
* 先序遍历-递归法
* 208/208 cases passed (8 ms)
Your runtime beats 27.56 % of java submissions
Your memory usage beats 25.28 % of java submissions (41.8 MB)
*/
// public List<String> binaryTreePaths(TreeNode root) {
// List<String> ans = new LinkedList<>();
// combineNodeVal(root, String.valueOf(root.val), ans);
// return ans;
// }
// private void combineNodeVal(TreeNode root, String str, List<String> ans) {
// // 当前节点为叶子节点
// if(root.left == null && root.right == null){
// ans.add(str);
// return;
// }
// if(root.left != null){
// combineNodeVal(root.left, str + "->" + root.left.val, ans);
// }
// if(root.right != null){
// combineNodeVal(root.right, str + "->" + root.right.val, ans);
// }
// }
/**
* 优化重复使用 buff 拼接字符串
* 208/208 cases passed (1 ms)
Your runtime beats 99.96 % of java submissions
Your memory usage beats 99.34 % of java submissions (41.3 MB)
*/
// public List<String> binaryTreePaths(TreeNode root) {
// List<String> ans = new LinkedList<>();
// StringBuilder buff = new StringBuilder();
// buff.append(root.val);
// combineNodeVal(root, buff, ans);
// // 清空buff
// buff.setLength(0);
// return ans;
// }
// private void combineNodeVal(TreeNode root, StringBuilder buff, List<String> ans) {
// // 当前节点为叶子节点
// if(root.left == null && root.right == null){
// ans.add(buff.toString());
// return;
// }
// if(root.left != null){
// int len = buff.length();
// buff.append("->")
// .append(root.left.val);
// combineNodeVal(root.left, buff, ans);
// buff.setLength(len);
// }
// if(root.right != null){
// int len = buff.length();
// buff.append("->")
// .append(root.right.val);
// combineNodeVal(root.right, buff, ans);
// buff.setLength(len);
// }
// }
// public List<String> binaryTreePaths(TreeNode root) {
// List<String> ans = new LinkedList<>();
// String str = String.valueOf(root.val);
// combineNodeVal(root.left, str, ans);
// combineNodeVal(root.right, str, ans);
// return ans;
// }
/**
* 先序遍历-迭代法
* 208/208 cases passed (2 ms)
Your runtime beats 51.05 % of java submissions
Your memory usage beats 94.6 % of java submissions (41.4 MB)
* */
public List<String> binaryTreePaths(TreeNode root) {
List<String> ans = new LinkedList<>();
StringBuilder buff = new StringBuilder();
Stack<TreeNode> stack = new Stack<TreeNode>();
// 当前节点在buff中的位置
Stack<Integer> posStack = new Stack<Integer>();
stack.push(root);
/**
* 利用null节点,来不断回溯,遍历可能存在的右子树
*/
while(stack.size() > 0){
TreeNode tn = stack.peek();
if(tn == null){
stack.pop();
if(stack.size() == 0){
break;
}
// 获取tn父节点
TreeNode parentTn = stack.peek();
if(parentTn.left == null && parentTn.right == null){
// 叶子节点
ans.add(buff.toString());
}
// 弹出父节点和它之后遍历的元素记录
stack.pop();
buff.setLength(posStack.peek());
posStack.pop();
// 继续访问右子树
stack.push(parentTn.right);
continue;
}
if(buff.length() != 0){
// 非头节点
buff.append("->");
}
// 记录元素和位置
buff.append(tn.val);
posStack.push(buff.length());
// 继续访问左子树
stack.push(tn.left);
}
return ans;
}
}
// @lc code=end