lintcode480-二叉树的所有路径

   给一棵二叉树,找出从根节点到叶子节点的所有路径。
   输入:
       1
      / \
    2   3
     \
      5
    输出:
   [
     "1->2->5",
     "1->3"
     ]

  Solution:非递归算法
  思路:题目需要 遍历二叉树所有的路径,该问题是深度优先搜索,广度优先所有借助于数据结构"队列",深度优先搜索则借助数据结构"栈",
 
    前序遍历的深度优先算法很简单,将所有左儿子入栈并打印直到左儿子为null,为null后弹出栈顶元素,即上一个节点,转到其右儿子
    本题需要遍历所有路径,那么就需要用pre保存上一个处理过的节点,然后在是否转向右子树的时候判断右子树是不是处理过了,
    如果没处理过,继续处理右子树,如果处理过了,就继续pop,直到右子树么有被处理过
 
    步骤:
    1、先将根入栈
    2、如果左儿子非null,入栈
    3、左儿子为空,取出栈顶元素,如果右儿子非空且没被处理过,继续处理右儿子
    4、否则,弹出栈顶元素,判断是否是叶子节点,是打印
    5、记录当前处理元素,当前处理元素置空
 

public class Q480_binaryTreePaths {
    
    public List<String> binaryTreePaths(TreeNode root) {
        // write your code here
        List<String> result = new ArrayList<String>();
        if(root == null)
            return result;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode current = root;
        TreeNode pre = null;
        while(current != null || !stack.isEmpty()) {
            if(current != null) {
                stack.push(current);
                current = current.left;
            } else {
                current = stack.peek();
                if(current.right != null && pre != current.right) {
                    current = current.right;
                } else {
                    current = stack.pop();
                    if(current.left == null && current.right == null) {
                        String res = "";
                        for(TreeNode t : stack) {
                            res = res + "->" + t.val;
                        }
                        res += "->" + current.val;
                        result.add(res.substring(2,res.length()));
                    }
                    pre = current;
                    current = null;
                }
            }
        }
        return result;
    }

    @Test
    public void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.right = new TreeNode(5);
        root.left.left = new TreeNode(4);
        root.right = new TreeNode(3);
        List<String> result = binaryTreePaths(root);
        System.out.println(result.toString());
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值