leetcode--257. Binary Tree Paths

1、问题描述

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"]

2、边界条件:root为null;

3、思路:递归,当前一个节点放入path中,左右子树分别继续往前走。左右子树只有当!= null时才继续找。

base case:左右子节点都为空;root节点 == null 两种情况。

不能用当前节点是否为空做base case,因为对于一个非空节点,如果有一个子节点,那么只有这个节点才是一条路径上的节点。

4、代码实现

方法一:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> results = new ArrayList<>();
        binaryTreePath(results, new ArrayList<Integer>(), root);
        return results;
    }

    public void binaryTreePath(List<String> results, List<Integer> path, TreeNode cur) {
        if (cur == null) {
            return; //root==null
        }
        if (cur.left == null && cur.right == null) {
            path.add(cur.val); //add cur's val
            String result = new String();
            result += path.get(0); //format
            for (int i = 1; i < path.size(); i++) {
                result += "->" + path.get(i);
            }
            results.add(result);
            path.remove(path.size() - 1); //recover
            return;
        }

        if (cur.left != null) {
            path.add(cur.val);
            binaryTreePath(results, path, cur.left);
            path.remove(path.size() - 1);
        }
        if (cur.right != null) {
            path.add(cur.val);
            binaryTreePath(results, path, cur.right);
            path.remove(path.size() - 1);
        }
    }
}

优化

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> results = new ArrayList<>();
        binaryTreePath(results, new ArrayList<Integer>(), root);
        return results;
    }

    public void binaryTreePath(List<String> results, List<Integer> path, TreeNode cur) {
        if (cur == null) {
            return; //root==null
        }
        if (cur.left == null && cur.right == null) {//需要在上层判断
            path.add(cur.val); //add cur's val
            String result = new String();
            result += path.get(0); //format
            for (int i = 1; i < path.size(); i++) {
                result += "->" + path.get(i);
            }
            results.add(result);
            path.remove(path.size() - 1); //recover
            return;
        }
        
        path.add(cur.val);
        if (cur.left != null) {
            binaryTreePath(results, path, cur.left);
        }
        if (cur.right != null) {
            binaryTreePath(results, path, cur.right);
        }
        path.remove(path.size() - 1);
    }
}

 方法二

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> results = new ArrayList<>();
        if (root == null) {
            return results;
        }
        binaryTreePath(results, "", root);
        return results;
    }

    public void binaryTreePath(List<String> results, String path, TreeNode cur) {//直接操作字符串
        if (cur.left == null && cur.right == null) {
            results.add(path + cur.val);
            return;
        }

        if (cur.left != null) {
            binaryTreePath(results, path + cur.val + "->", cur.left);
        }
        if (cur.right != null) {
            binaryTreePath(results, path + cur.val + "->", cur.right);
        }
    }
}

 

5、api:

result += path.get(0); ArrayList<Integer> path.

posted on 2017-08-31 09:52  Shihu  阅读(170)  评论(0编辑  收藏  举报

导航