257. 二叉树的所有路径

257. 二叉树的所有路径icon-default.png?t=M666https://leetcode.cn/problems/binary-tree-paths/

class Solution {
	public List<String> binaryTreePaths(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		if (root != null) {
			stack.push(root);
		}
		Stack<String> paths = new Stack<>();
		paths.push(String.valueOf(root.val));
		List<String> ans = new ArrayList<>();
		while (!stack.isEmpty()) {
			TreeNode node = stack.pop();
			System.out.println(paths.toString() + " " + ans + " " + (node == null ? stack.peek().val : node.val));
			if (node != null) {
				String path = paths.pop();
				if (node.left == null && node.right == null) {
					ans.add(path);
				}
				if (node.right != null) {
					paths.push(path + "->" + node.right.val);
					stack.push(node.right);
				}
				if (node.left != null) {
					paths.push(path + "->" + node.left.val);
					stack.push(node.left);
				}
				stack.push(node);
				stack.push(null);
			} else {
				TreeNode pop = stack.pop();
				if (pop.left == null && pop.right == null) {
					ans.add(paths.pop());
				}
			}
		}
		return ans;
	}
}

 class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        if (root != null) {
            stack.push(root);
        }
        Stack<String> paths = new Stack<>();
        paths.push(String.valueOf(root.val));
        List<String> ans = new ArrayList<>();
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            System.out.println(paths.toString() + " " + ans + " " + (node == null ? stack.peek().val : node.val));
            if (node != null) {
                String path = paths.pop();
                if (node.left == null && node.right == null) {
                    ans.add(path);
                }
                if (node.right != null) {
                    paths.push(path + "->" + node.right.val);
                    stack.push(node.right);
                }
                if (node.left != null) {
                    paths.push(path + "->" + node.left.val);
                    stack.push(node.left);
                }
                stack.push(node);
                stack.push(null);
            } else {
                TreeNode pop = stack.pop();
            }
        }
        return ans;
    }
}
这里要注意下,之所以不在else里加答案,主要是因为,会重复if里的操作,还有为什么只要在if里加答案,是因为,每次if迭代推进栈的实际上是当前点的下一个点路径,这导致了,如果不在if里添加答案,当当前点被废弃时,它的路径path也会被废弃,这导致了尽管else可以遍历到每一个节点,但它对应的路径却已经在if中被消掉了
可以这么理解,实际上if是用来添加stack里的路径点的,并标记了应该如何出栈,同时最后也回收了弹出栈的点,else就是对栈内的点进行回收处理,这道题最大的问题就是paths里储存的信息和stack里的不一样,stack储存的是当前点,但是path里储存的却是当前点的左右子点,这产生了矛盾
class Solution {
    /**
     * 迭代法
     */
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> result = new ArrayList<>();
        if (root == null)
            return result;
        Stack<Object> stack = new Stack<>();
        // 节点和路径同时入栈
        stack.push(root);
        stack.push(root.val + "");
        while (!stack.isEmpty()) {
            // 节点和路径同时出栈
            String path = (String) stack.pop();
            TreeNode node = (TreeNode) stack.pop();
            // 若找到叶子节点
            if (node.left == null && node.right == null) {
                result.add(path);
            }
            //右子节点不为空
            if (node.right != null) {
                stack.push(node.right);
                stack.push(path + "->" + node.right.val);
            }
            //左子节点不为空
            if (node.left != null) {
                stack.push(node.left);
                stack.push(path + "->" + node.left.val);
            }
        }
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cjz-lxg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值